简体   繁体   English

快速解析如何将对象保存在本地数据存储中并显示它们?

[英]swift parse how to save objects in local datastore and show them?

I've enabled local datastore in app delegate , I've did a query but how can i save that objects to view them if there's no internet ? 我已经local datastore in app delegate启用了local datastore in app delegate ,已经进行了查询,但是如果no internet ,如何保存该对象以查看它们?

Here is my query : 这是我的查询:

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

Now how can i do a local query to view this objects ? 现在如何进行local query以查看此objects

Before calling findObjectsInBackgroundWithBlock , 在调用findObjectsInBackgroundWithBlock之前,

query.fromLocalDatastore()

This will force your query to pull from localDataStore. 这将强制您的查询从localDataStore中提取。 Your code should look like below: 您的代码应如下所示:

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

In order to save objects locally, you must first enable the localDataStore: 为了将对象保存在本地,必须首先启用localDataStore:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    Parse.enableLocalDatastore()
    Parse.setApplicationId("parseAppId", clientKey: "parseClientKey")
  }
}

The recommended way for saving objects with localDataStore is to use saveEventually : 建议使用saveEventually保存对象的localDataStore是使用saveEventually

myObject.saveEventually()

You may also use .pinInBackground() to keep objects in the localDataStore "permanently". 您还可以使用.pinInBackground()将对象“永久”保留在localDataStore中。 Pinning objects makes sure that local objects are automatically updated when you fetch them again from the server. 固定对象可确保在您再次从服务器获取本地对象时自动更新它们。 It is an ideal way to implement offline caching of server data. 这是实现服务器数据的脱机缓存的理想方法。

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

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