3

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 ?

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 ?

Bannings
  • 10,376
  • 7
  • 44
  • 54
Salah
  • 933
  • 3
  • 13
  • 32
  • you can call `saveInBackgroundWithBlock` method to the PFObject to save it to the local database, remember local database must be enabled in AppDelegate. And to retrieving data, create PFQuery Object and call `findObjectsInBackgroundWithBlock` method on the same. – Suryakant Sharma Aug 25 '15 at 13:03
  • @SuryakantSharma when should i call saveInBackgroundWithBlock? inside findObjectsInBackgroundWithBlock or when? – Salah Aug 25 '15 at 13:16
  • 1
    no! `findObjectsInBackgroundWithBlock` is for fetching object from Parse DB using `PFQuery` and `saveInBackgroundWithBlock` is for saving. – Suryakant Sharma Aug 25 '15 at 13:22
  • @SuryakantSharma Could you answer to my question and show me a simple code to get username with query and save it then display it on cell also if there's no internet please? – Salah Aug 25 '15 at 13:26
  • First tell me, where did you stuck, what have you done so far? – Suryakant Sharma Aug 25 '15 at 13:30
  • @SuryakantSharma Actually I don't know how the local data working, I've did a query like in my question to get username and photo and then display them on cells, but each time i need to make query to get them again, i just wanna make query once then always get the data from local. – Salah Aug 25 '15 at 13:35
  • Have you found the answer? – AAA Dec 29 '15 at 18:33
  • @AAA I don't think so, I want to show results without internet, like save last object and show it if there's no internet, any idea ? – Salah Dec 30 '15 at 10:25
  • Sorry, No. I am also looking for the same answer! – AAA Dec 30 '15 at 10:37

1 Answers1

4

Before calling findObjectsInBackgroundWithBlock,

query.fromLocalDatastore()

This will force your query to pull from 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:

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:

myObject.saveEventually()

You may also use .pinInBackground() to keep objects in the localDataStore "permanently". 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.

mrbcg
  • 371
  • 1
  • 7
  • thank you, I did that but i didn't got any results because local data store is empty, Can you edit your answer and do how to save objects in local data store? – Salah Jan 01 '16 at 17:52