简体   繁体   English

解析本地数据存储+网络同步

[英]Parse Local Datastore + Network Sync

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 : 从6个月开始,我就在我的iOS应用程序(使用Swift编写)中使用Parse.com,出于多种原因,我想使用Parse Local Datastore:

  • Make my application usable (retrievable) offline 使我的应用程序可以离线使用(可检索)
  • Reduce data usage (many queries returning « non updated data ») 减少数据使用(许多查询返回“未更新的数据”)
  • Reduce loading time (mainly when starting application and loading all data from network) 减少加载时间(主要是在启动应用程序并从网络加载所有数据时)

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 :) 我已经对该函数应该做什么有一个具体的想法,但是我不知道如何从技术上编写该函数:)

Scenarios : 场景:

  • Sign Up / Log In (chain multiple query) : 注册/登录(链接多个查询):

    1. Get data from Network 从网络获取数据
    2. Save date inside « lastUpdateLocalDatastore » variable in NSUserDefaults 将日期保存在NSUserDefaults中的“ lastUpdateLocalDatastore”变量中
    3. Pin data in Local Datastore 在本地数据存储区中固定数据
    4. Display data from Local Datastore —> RETURN & update TableView 显示本地数据存储中的数据->返回并更新TableView
  • Loading App (chain multiple query) : 加载应用程序(链式多重查询):

    1. Display data from Local Datastore —> RETURN & update TableView 显示本地数据存储中的数据->返回并更新TableView
    2. Get data from Network (where « lastUpdateDate » in Parse is newer than « lastUpdateLocalDatastore » from NSUserDefault) 从网络获取数据(解析中的“ lastUpdateDate”比NSUserDefault中的“ lastUpdateLocalDatastore”新)
    3. Pin data in Local Datastore 在本地数据存储区中固定数据
    4. Display updated data from Local Datastore —> RETURN & update TableView 显示本地数据存储中的更新数据->返回并更新TableView
  • Trigger update (simple query) : 触发更新(简单查询):

    1. Get data from Network (where « lastUpdateDate » in Parse is newer than « lastUpdateLocalDatastore » from NSUserDefault) 从网络获取数据(解析中的“ lastUpdateDate”比NSUserDefault中的“ lastUpdateLocalDatastore”新)
    2. Pin data in Local Datastore 在本地数据存储区中固定数据
    3. Display updated data from Local Datastore —> RETURN & update TableView 显示本地数据存储中的更新数据->返回并更新TableView
  • Log Out : 登出 :

    1. Unpin all data in Local Datastore 取消固定本地数据存储区中的所有数据
    2. Clear « lastUpdate » values in NSUserDefault 清除NSUserDefault中的«lastUpdate»值

Function structure : 功能结构:

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
    }
}

Problems : 问题 :

  1. How to handle multiple (asynchronous) returns 如何处理多个(异步)返回
  2. How to make a function capable of chaining multiple queries (for example I need to retrieve data from 6 different queries when I load my app) 如何使函数能够链接多个查询(例如,加载应用程序时,我需要从6个不同的查询中检索数据)

Finally I found a way to do it based on this GitHub topic : https://github.com/ParsePlatform/ParseUI-iOS/issues/53 最后,我找到了一个基于此GitHub主题的方法: https : //github.com/ParsePlatform/ParseUI-iOS/issues/53

Here is the function I use : 这是我使用的功能:

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

TO DO : I need to add the "lastUpdateDate" option to fetch only modified data from network. 要做:我需要添加“ lastUpdateDate”选项以仅从网络获取修改后的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM