[英]Titanium Alloy Caching in Android/iOS? Or Preserving old views
Can we Cache Dynamically Created Lists or View till the webservices are called in background. 我们可以缓存动态创建的列表或查看,直到在后台调用Web服务。 I want to achieve something like the FaceBook App does.
我想实现像FaceBook App那样的东西。 I know its possible in Android Core but wanted to try it in Titanium (Android and IOS).
我知道它可能在Android Core中,但想在Titanium(Android和IOS)中尝试它。
I would further explain it, Consider I have a app which has a list. 我会进一步解释它,考虑我有一个有列表的应用程序。 Now When I open for first time, it will obviously hit the webservice and create a dynamic list.
现在当我第一次打开时,它显然会点击web服务并创建一个动态列表。
Now I close the app and again open the app. 现在我关闭应用程序并再次打开应用程序。 The old list should be visible till the webservice provides any data.
在webservice提供任何数据之前,旧列表应该是可见的。
Yes Titanium can do this. 是的Titanium可以做到这一点。 You should use a global variable like
Ti.App.myList
if it is just an array / a list / a variable. 如果它只是一个数组/列表/变量,你应该使用像
Ti.App.myList
这样的全局变量。 If you need to store more complex data like images or databases you should use the built-in file system. 如果您需要存储更复杂的数据,如图像或数据库,则应使用内置文件系统。 There is a really good Documentation on the Appcelerator website .
Appcelerator网站上有一个非常好的文档。
The procedure for you would be as follows: 您的程序如下:
You should consider to implement some variable to check wether any update is needed to minimize the network use (it saves energy and provides a better user experience if the users internet connection is slow). 您应该考虑实施一些变量来检查是否需要更新以最小化网络使用(如果用户的互联网连接速度慢,它可以节省能源并提供更好的用户体验)。
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();
}
}
In addition to this code which should be placed in a success method of an API call, you need a global variable Ti.App.imageMap
to store the map of keys and the corresponding urls. 除了应该放在API调用的成功方法中的代码之外,还需要一个全局变量
Ti.App.imageMap
来存储密钥映射和相应的URL。 I guess you have to change the code a bit to fit your needs and your project but it should give you a good starting point. 我想你必须稍微改变代码以满足你的需求和项目,但它应该给你一个很好的起点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.