4

I'd like to save all my objects both locally and in cloud and read from Local Datastore only. All my objects should be saved in both local and online store and local datastore should be synced to Parse Cloud.

I want my App to work Offline so as to use it Everywhere, but saving all the data in the Cloud asap the NetConnection is Available !

Thanks

Kingofmit
  • 2,066
  • 1
  • 17
  • 22

1 Answers1

3

First of all you need to enable the use of local datastore:

[Parse enableLocalDatastore];

Next, I always save a new PFObject to the local datastore using

PFObject *userStat;
[userStat saveEventually];

This will both pin your object to the local datastore and save it to cloud (eventually). If you want to reset all of your locally stored data with what you have in the cloud, you can; first unpin all your local objects and then fetch all the remote objects and pin them locally:

[PFObject unpinAllObjects];
PFQuery *query = [PFQuery queryWithClassName:@"UserStats"];
[query whereKey:@"parent" matchesQuery:query];

    return [[query findObjectsInBackground] continueWithBlock:^id(BFTask *task) {
        if (task.error){
             return nil;
        }
         return [[PFObject pinAllInBackground:task.result]     continueWithBlock:^id(BFTask *task) {
            return task;
        }];
}];

Local queries can be done using:

 [query fromLocalDatastore];

Parse has good documentation of these methods here.

Trond Kristiansen
  • 2,379
  • 23
  • 48
  • Thanks for the aswer it Works !!! But I can't save eventually a pFfile...how do you proceed in that case to save a picture offline ??? Thanks – Kingofmit May 10 '15 at 11:52
  • i Get this Error [Error]: Caught "NSInternalInconsistencyException" with reason "Unable to saveEventually a PFObject with a relation to a new, unsaved PFFile." – Kingofmit May 10 '15 at 13:09
  • Don't really know what causes your error but look at the disucssions here: http://stackoverflow.com/questions/28136175/image-within-pffile-will-be-uploaded-to-parse-instead-of-being-saved-into-the-lo and here http://stackoverflow.com/questions/28132351/save-eventually-on-pfobject-with-pffile-parse-local-datastore – Trond Kristiansen May 10 '15 at 22:03
  • Check this out http://stackoverflow.com/questions/30213808/how-do-i-save-an-image-pffile-in-localdatastore-while-offline-parse-com – Kingofmit May 13 '15 at 14:56