解析本地数据存储+网络同步 [英] Parse Local Datastore + Network Sync
问题描述
我在6个月后使用Parse.com和我的iOS应用程序(用Swift编写),我想使用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) :
- 从网络获取数据
- 在«lastUpdateLocalDatastore»中保存日期NSUserDefaults中的变量
- 本地数据存储区中的引脚数据
- 显示本地数据存储区中的数据 - > RETURN&更新TableView
- Get data from Network
- Save date inside « lastUpdateLocalDatastore » variable in NSUserDefaults
- Pin data in Local Datastore
- Display data from Local Datastore —> RETURN & update TableView
加载应用程序(链式多个查询):
Loading App (chain multiple query) :
- 显示本地数据存储区中的数据 - > RETURN&更新TableView
- 从网络获取数据(其中Parse中的lastUpdateDate比NSUserDefault中的lastUpdateLocalDatastore更新)
- 本地数据存储区中的引脚数据
- 显示本地数据存储的更新数据 - > RETURN&更新TableView
- Display data from Local Datastore —> RETURN & update TableView
- Get data from Network (where « lastUpdateDate » in Parse is newer than « lastUpdateLocalDatastore » from NSUserDefault)
- Pin data in Local Datastore
- Display updated data from Local Datastore —> RETURN & update TableView
触发更新(简单查询):
Trigger update (simple query) :
- 从网络获取数据(其中Parse中的lastUpdateDate比NSUserDefault的lastUpdateLocalDatastore更新)
- 本地数据存储区中的引脚数据
- 显示本地数据存储区的更新数据 - > RETURN&更新TableView
退出:
Log Out :
- 取消固定本地数据存储区中的所有数据
- 清除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
}
}
问题:
Problems :
- 如何处理多个(异步)返回
- 如何使函数能够链接多个查询(例如我我加载应用程序时需要从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屋!