Synchronization between Parse and localDataStore

I had a problem synchronizing my local data with Parse data when using 'enableLocalDataStore'. If I do not use local storage, everything is fine, but I would like to minimize calls to the Parse server. I understand that if I use aaveEventually 'for newly created objects, they will be saved (pinned) locally and synchronized with Parse when an Internet connection is available. This also works great. My problem is that I don’t know the best way to update the local "dataStore" with Parse "dataStore", except to call a method that removes the changes remotely and is updated locally. Currently use the following:

-(void) fetchAllFavorites{ PFQuery *query = [PFQuery queryWithClassName:@"UserStats"]; [query fromLocalDatastore]; [[query findObjectsInBackground] continueWithBlock:^id(BFTask *task) { if (task.error) { } else { [PFObject pinAll:task.result]; } return task.result; }]; } 

This approach does not take into account changes that may have occurred in the Parse 'dataStore'. I could just turn off all local objects and get everything that is in Parse by calling the method directly. However, I would have thought that there would be a more efficient approach that would easily synchronize local changes with changes in Parse 'dataStore'? Isn't that an idea to use "localDataStore" in the first place? Currently, I only see that this works in one direction: you save the data locally and then update the Parse 'dataStore' manually, but you do not synchronize them with each other. At least this is an idea that I am getting from some examples , and I am wondering if anyone has a good approach to how: how to enable 'localDataStore' which is continuously synchronized with Parse 'dataStore'? Any suggestions and examples would be very helpful. I am programming in Cocoa, but Java examples will also be great. Thanks T.

+6
source share
2 answers

My solution was to make a local request, and if there was nothing, then request a server. On my settings page, I have a function that will "synchronize" with the server, completely update pinned objects basically. This is not necessary, because every time the user edits, deletes or adds an object, I call saveEventually , deleteEventually , pinInBackground , unpinInBackground , if necessary. My launch function looks like this. If no local objects are found, the server is requested and they are bound so that next time it will be only local.

 PFQuery *localQuery = [PFQuery queryWithClassName:@"ClassName"]; [localQuery fromLocalDatastore]; // and any other constraints [localQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if(objects.count > 0) { self.objects = objects; } else { PFQuery *remoteQuery = [PFQuery queryWithClassName:@"ClassName"]; [remoteQuery findObjectsInBackground:^(NSArray *objects, NSError *error) { if (objects) { self.objects = objects; [PFObject pinAllInBackground:objects]; } }]; } }]; 
+1
source

My approach was to write a utility that checks if something has been updated or deleted in the cloud. Every time I needed to perform an update, I called:

 [[ParseLocalDataStoreManager sharedInstance]updateClasses:classes]; //classes is a NSArray 

I kept track of which classes were already in localStore and which weren't. If the class was not, then I requested and bound all its objects. If that were the case, I would only update the objects that were changed in the cloud after the last date I updated. I used Parse's

 [query whereKey:@"updatedAt" greaterThanOrEqualTo:lastUpdated]; 

and saved the lastUpdated entry in "NSUserDefaults".

To remove objects that were no longer in the cloud, I wrote a small cloud function that returned objects in the cloud for a specific class. Then I compared them to what I had in localStore, and deleted those that were not in the Cloud.

In my case, I update every time the user opens the application, since it did not require a real-time update. But if you need real-time updates, you can set a timer and call the function update with the classes you want to update in real time.

I downloaded the utility on github: https://github.com/timpalade/ParseLocalDataStoreManager

You can find instructions on github as well as cloud code. Hope this helps!

0
source

Source: https://habr.com/ru/post/981377/