簡體   English   中英

快速解析如何將對象保存在本地數據存儲中並顯示它們?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM