【问题标题】:Titanium Alloy Caching in Android/iOS? Or Preserving old viewsAndroid/iOS 中的钛合金缓存?或保留旧视图
【发布时间】:2015-01-01 10:29:15
【问题描述】:

我们可以缓存动态创建的列表或视图,直到后台调用 web 服务。我想实现类似 FaceBook 应用程序所做的事情。我知道它在 Android Core 中是可能的,但想在 Titanium(Android 和 IOS)中尝试。

我会进一步解释, 考虑我有一个有列表的应用程序。现在当我第一次打开时,它显然会点击 web 服务并创建一个动态列表。

现在我关闭应用程序并再次打开应用程序。在 web 服务提供任何数据之前,旧列表应该是可见的。

【问题讨论】:

  • 您使用的是什么数据格式? json?你对从网络服务返回的数据做了什么?
  • @developer82 是的,我的数据格式是 json。我正在循环遍历 json 对象并填充我的列表并为 imageview 设置路径。
  • 也许您可以将数据存储在本地数据库中..
  • @murli2308 它不会增加我的应用程序的大小。

标签: web-services caching titanium titanium-mobile titanium-alloy


【解决方案1】:

是的,Titanium 可以做到这一点。如果它只是一个数组/列表/变量,您应该使用像 Ti.App.myList 这样的全局变量。如果您需要存储更复杂的数据,例如图像或数据库,您应该使用内置文件系统。 Appcelerator website 上有一个非常好的文档。

您的程序如下:

  1. 首次加载数据
  2. 以您喜欢的方式存储您的数据(全局变量、文件系统)
  3. 在未来的应用程序启动期间,读出您的本地列表/数据并显示它,直到您的同步成功。

您应该考虑实施一些变量来检查是否需要任何更新以最大限度地减少网络使用(如果用户的互联网连接速度很慢,它可以节省能源并提供更好的用户体验)。

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();
        }
    }

除了应该放在 API 调用的成功方法中的这段代码之外,您还需要一个全局变量 Ti.App.imageMap 来存储键的映射和相应的 url。我想您必须稍微更改代码以适应您的需求和项目,但它应该为您提供一个良好的起点。

【讨论】:

  • 如何管理这里面的图片。我的列表也有远程存储的图像。在 Web 环境中,浏览器会缓存图像或在 phonegap 中。但是钛会发生什么
  • 在 Titanium 中,您必须迭代 API 调用的响应。然后将每个图像存储在您选择的目录中。稍后我会提供一些代码。
  • 我急切地等待图像缓存代码。我能够在数据库中缓存网络服务。
  • 坏主意!在 Ti.App 上保留您自己的对象是一种非常糟糕的做法 - 您正在做所谓的“过桥”,这对您的应用程序的性能和内存都不利。
  • 使用某种持久性 - 保存到 sqllite,序列化到文件,使用 noSQL 数据库。并将全局参数存储在您的应用程序中,封装在 commonJS 中 - 在 Appcelerator 文档中甚至建议将其作为最佳实践。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多