[英]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:
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) : 注册/登录(链接多个查询):
Loading App (chain multiple query) : 加载应用程序(链式多重查询):
Trigger update (simple query) : 触发更新(简单查询):
Log Out : 登出 :
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
}
}
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.