解析本地数据存储 + 网络同步 [英] Parse Local Datastore + Network Sync

查看:29
本文介绍了解析本地数据存储 + 网络同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自 6 个月以来,我在我的 iOS 应用程序(用 Swift 编写)中使用 Parse.com,出于多种原因,我想使用 Parse 本地数据存储:

I am using Parse.com with my iOS application (written in Swift) since 6 month and I would like to use Parse local Datastore for many reasons :

  • 使我的应用程序可离线使用(可检索)
  • 减少数据使用(许多查询返回«未更新的数据»)
  • 减少加载时间(主要是在启动应用程序和从网络加载所有数据时)

为了做到这一点,我想编写一个全局函数来处理我在应用程序中执行的所有查询的这些场景.

In order to do so I would like to write a global function handeling these scenarios for all the query I do from my application.

我已经对函数应该做什么有了具体的想法,但我不知道如何在技术上编写这个函数:)

I already have a specific idea of what the function should do but I don’t know how to technically write this function :)

  • 注册/登录(链接多个查询):

  • Sign Up / Log In (chain multiple query) :

  1. 从网络获取数据
  2. 在 NSUserDefaults 中的 « lastUpdateLocalDatastore » 变量中保存日期
  3. 在本地数据存储区固定数据
  4. 显示来自本地数据存储区的数据 —> RETURN &更新 TableView

  • 正在加载应用程序(链接多个查询):

  • Loading App (chain multiple query) :

    1. 显示来自本地数据存储区的数据 —> RETURN &更新 TableView
    2. 从网络获取数据(其中 Parse 中的 «lastUpdateDate» 比 NSUserDefault 中的 «lastUpdateLocalDatastore» 新)
    3. 在本地数据存储区固定数据
    4. 显示来自本地数据存储区的更新数据 —> RETURN &更新 TableView

  • 触发更新(简单查询):

  • Trigger update (simple query) :

    1. 从网络获取数据(其中 Parse 中的 «lastUpdateDate» 比 NSUserDefault 中的 «lastUpdateLocalDatastore» 新)
    2. 在本地数据存储区固定数据
    3. 显示来自本地数据存储区的更新数据 —> RETURN &更新 TableView

  • 退出:

  • Log Out :

    1. 取消固定本地数据存储区中的所有数据
    2. 清除 NSUserDefault 中的 « lastUpdate » 值

  • IF ( "First login" -> Local Datastore is empty ) {
    
        Get data from Network
        Pin data in Local Datastore
        Save « lastUpdateLocalDatastore » in NSUSerDefaults
        —> RETURN data in Cache
    
    } ELSE {
    
        IF ( "Launching application" -> Cache is empty ) {
            Get data from Local Datastore
            —> RETURN data in Cache
        } ELSE IF ( "trigger update" ) {
           Get data from Network
           Pin new data in Local Datastore
           Save « lastUpdateLocalDatastore » in NSUSerDefaults
           —> RETURN data in Cache
        }
    }
    

    问题:

    1. 如何处理多个(异步)返回
    2. 如何使函数能够链接多个查询(例如,我需要在加载应用程序时从 6 个不同的查询中检索数据)

    推荐答案

    最后我找到了一种基于这个 GitHub 主题的方法:https://github.com/ParsePlatform/ParseUI-iOS/issues/53

    Finally I found a way to do it based on this GitHub topic : https://github.com/ParsePlatform/ParseUI-iOS/issues/53

    这是我使用的函数:

    func findObjectsLocallyThenRemotely(query: PFQuery!, block:[AnyObject]! -> Void) {
    
        let localQuery = (query.copy() as! PFQuery).fromLocalDatastore()
        localQuery.findObjectsInBackgroundWithBlock({ (locals, error) -> Void in
            if (error == nil) {
                println("Success : Local Query", msg: "(query.parseClassName)")
                block(locals)
            } else {
                println("Error : Local Query", msg: "(query.parseClassName)", error: error)
            }
            query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
                if(error == nil) {
                    println("Success : Network Query", msg: "(query.parseClassName)")
                    PFObject.unpinAllInBackground(locals, block: { (success, error) -> Void in
                        if (error == nil) {
                            println("Success : Unpin Local Query", msg: "(query.parseClassName)")
                            block(objects)
                            PFObject.pinAllInBackground(objects, block: { (success, error) -> Void in
                                if (error == nil) {
                                    println("Success : Pin Query Result", msg: "(query.parseClassName)")
                                } else {
                                    println("Error : Pin Query Result", msg: "(query.parseClassName)", error: error)
                                }
                            })
                        } else {
                            println("Error : Unpin Local Query", msg: "(query.parseClassName)", error: error)
                        }
                    })
                } else {
                    println("Error : Network Query", msg: "(query.parseClassName)", error: error)
                }
            })
        })
    }
    

    待办事项:我需要添加lastUpdateDate"选项以仅从网络获取修改后的数据.

    TO DO : I need to add the "lastUpdateDate" option to fetch only modified data from network.

    这篇关于解析本地数据存储 + 网络同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆