![](/img/trans.png)
[英]how to use local datastore to save and query data with parse and swift
[英]swift parse how to save objects in local datastore and show them?
我已经local datastore in app delegate
启用了local datastore in app delegate
,已经进行了查询,但是如果no internet
,如何保存该对象以查看它们?
这是我的查询:
query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
if error == nil && objects != nil
{
self.messages = objects!
for object in objects! {
inboxphotos.append(object.objectForKey("photo") as! PFFile)
inboxusers.append(object.objectForKey("username") as! String) }
}
}
现在如何进行local query
以查看此objects
?
在调用findObjectsInBackgroundWithBlock
之前,
query.fromLocalDatastore()
这将强制您的查询从localDataStore中提取。 您的代码应如下所示:
query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
if error == nil && objects != nil
{
self.messages = objects!
for object in objects! {
inboxphotos.append(object.objectForKey("photo") as! PFFile)
inboxusers.append(object.objectForKey("username") as! String) }
}
}
为了将对象保存在本地,必须首先启用localDataStore:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.enableLocalDatastore()
Parse.setApplicationId("parseAppId", clientKey: "parseClientKey")
}
}
建议使用saveEventually
保存对象的localDataStore
是使用saveEventually
:
myObject.saveEventually()
您还可以使用.pinInBackground()
将对象“永久”保留在localDataStore中。 固定对象可确保在您再次从服务器获取本地对象时自动更新它们。 这是实现服务器数据的脱机缓存的理想方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.