Web services Android/iOS中的钛合金缓存?还是保留旧观点
我们可以缓存动态创建的列表或视图,直到在后台调用Web服务。我想实现类似FaceBook应用程序的目标。我知道它在安卓内核中是可能的,但我想在钛合金(安卓和IOS)中试试 我会进一步解释,, 考虑一下我有一个有列表的应用程序。现在,当我第一次打开时,它显然会访问Web服务并创建一个动态列表Web services Android/iOS中的钛合金缓存?还是保留旧观点,web-services,caching,titanium,titanium-mobile,titanium-alloy,Web Services,Caching,Titanium,Titanium Mobile,Titanium Alloy,我们可以缓存动态创建的列表或视图,直到在后台调用Web服务。我想实现类似FaceBook应用程序的目标。我知道它在安卓内核中是可能的,但我想在钛合金(安卓和IOS)中试试 我会进一步解释,, 考虑一下我有一个有列表的应用程序。现在,当我第一次打开时,它显然会访问Web服务并创建一个动态列表 现在我关闭应用程序,然后再次打开应用程序。在Web服务提供任何数据之前,旧列表都应该是可见的。是的,钛可以做到这一点。如果它只是一个数组/列表/变量,则应该使用全局变量,如Ti.App.myList。如果需要
现在我关闭应用程序,然后再次打开应用程序。在Web服务提供任何数据之前,旧列表都应该是可见的。是的,钛可以做到这一点。如果它只是一个数组/列表/变量,则应该使用全局变量,如
Ti.App.myList
。如果需要存储更复杂的数据,如图像或数据库,则应使用内置的文件系统。有一个非常好的文档关于
你的程序如下:
您应该考虑实现一些变量来检查是否需要任何更新来最小化网络使用(如果用户上网速度较慢,则节省能量并提供更好的用户体验)。
除了这个应该放在API调用成功方法中的代码之外,您还需要一个全局变量
Ti.App.imageMap
来存储键的映射和相应的URL。我想您必须对代码进行一些更改,以满足您的需求和项目,但这应该会为您提供一个良好的起点。我如何在此环境中管理图像。我的列表中还有图像,这些图像被远程存储。在web环境中,浏览器会缓存图像或phonegap。但是在titaniumIn Titanium中发生了什么,您必须迭代API调用的响应。然后将每个图像存储在您选择的目录中。稍后我将提供一些代码。我急切地等待图像缓存代码。我能够在数据库中缓存Web服务。这不是个好主意!将自己的对象保留在Ti.App上是一种非常糟糕的做法-你正在做所谓的“过桥”,这对应用程序的性能和内存都有害。使用某种持久性-保存到sqllite,序列化到文件,使用noSQL db。对于在应用程序中存储全局参数,请封装在commonJS中——Appcelerator文档甚至建议将其作为最佳实践。您使用的是什么数据格式?json?您对从Web服务返回的数据做了什么?@developer82是的,我的数据格式是json。我正在json对象中循环,并填写我的列表和设置imageview的路径。可能你可以将数据存储在本地数据库中。@murli2308它不会增加我的应用程序的大小。
if (response.state == "SUCCESS") {
Ti.API.info("Themes successfully checked");
Ti.API.info("RESPONSE TEST: " + response.value);
//Create a map of the layout names(as keys) and the corresponding url (as value).
var newImageMap = {};
for (var key in response.value) {
var url = response.value[key];
var filename = key + ".jpg"; //EDIT your type of the image
newImageMap[filename] = url;
}
if (Ti.App.ImageMap.length > 0) {
//Check for removed layouts
for (var image in Ti.App.imageMap) {
if (image in newImageMap) {
Ti.API.info("The image " + image + " is already in the local map");
//Do nothing
} else {
//Delete the removed layout
Ti.API.info("The image " + image + " is deleted from the local map");
delete Ti.App.imageMap[image];
}
}
//Check for new images
for (var image in newImageMap) {
if (image in Ti.App.imageMap) {
Ti.API.info("The image " + image + " is already in the local map");
//Do nothing
} else {
Ti.API.info("The image " + image + " is put into the local map");
//Put new image in local map
Ti.App.imageMap[image] = newImageMap[image];
}
}
} else {
Ti.App.imageMap = newImageMap;
}
//Check wether the file already exists
for (var key in response.value) {
var url = response.value[key];
var filename = key + ".png"; //EDIT YOUR FILE TYPE
Ti.API.info("URL: " + url);
Ti.API.info("FILENAME: " + filename);
imagesOrder[imagesOrder.length] = filename.match(/\d+/)[0]; //THIS SAVES THE FIRST NUMBER IN YOUR FILENAME AS ID
//Case1: download a new image
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "/media/" + filename);
if (file.exists()) {
// Do nothing
Titanium.API.info("File " + filename + " exists");
} else {
// Create the HTTP client to download the asset.
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function() {
if (xhr.status == 200) {
// On successful load, take that image file we tried to grab before and
// save the remote image data to it.
Titanium.API.info("Successfully loaded");
file.write(xhr.responseData);
Titanium.API.info(file);
Titanium.API.info(file.getName());
};
};
// Issuing a GET request to the remote URL
xhr.open('GET', url);
// Finally, sending the request out.
xhr.send();
}
}