2

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

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

为此,我想编写一个全局函数来处理我从应用程序中执行的所有查询的这些场景。

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

场景:

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

    1. 从网络获取数据
    2. 在 NSUserDefaults 中的 « lastUpdateLocalDatastore » 变量中保存日期
    3. 在本地数据存储中固定数据
    4. 显示来自本地数据存储区的数据 —> 返回并更新 TableView
  • 加载应用程序(链多个查询):

    1. 显示来自本地数据存储区的数据 —> 返回并更新 TableView
    2. 从网络获取数据(其中 Parse 中的 «lastUpdateDate » 比 NSUserDefault 中的 «lastUpdateLocalDatastore » 更新)
    3. 在本地数据存储中固定数据
    4. 显示本地数据存储中的更新数据 —> 返回并更新 TableView
  • 触发更新(简单查询):

    1. 从网络获取数据(其中 Parse 中的 «lastUpdateDate » 比 NSUserDefault 中的 «lastUpdateLocalDatastore » 更新)
    2. 在本地数据存储中固定数据
    3. 显示本地数据存储中的更新数据 —> 返回并更新 TableView
  • 登出 :

    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 个不同的查询中检索数据)
4

1 回答 1

5

最后,我找到了一种基于此 GitHub 主题的方法: 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)
            }
        })
    })
}

TO DO:我需要添加“lastUpdateDate”选项以仅从网络中获取修改后的数据。

于 2015-08-20T09:50:30.230 回答