PFIncrementalStore
PFIncrementalStore copied to clipboard
Object is fetched again from Parse while saving the managed object context
Hi,
I am making my first experiments with PFIncrementalStore. I got a first test project up and running really quickly: I love the approach and really appreciate your work. Though I am struggling with a sync bug that I can't get rid of, and I don't understand if it is a bug or if I am approaching PFIncrementalStore the wrong way.
So, here is what is happening. I correctly retrieve some data from Parse, make some changes to the data, call save:
on the main managedObjectContext
. The issue is that sometimes the data is correctly saved, sometimes not, rolling back to the previous value.
I tried to debug this behavior: I added a symbolic breakpoint to -[PFQuery getObjectInBackgroundWithId:block:]
and as I suspected this is being called twice:
- directly by the library in
-updateObject:fromRequest:inContext:error:
override (this is correct) - indirectly by CoreData through
newValuesForObjectWithID:withContext:error:
:
#2 0x00007fff8c8e589a in -[NSPersistentStoreCoordinator(_NSInternalMethods) _conflictsWithRowCacheForObject:withContext:andStore:] ()
As I read it: the call to save:
immediately propagates the changes to the SQLite persistent store before the changes has been saved on the cloud. Of course CoreData find a conflict between the data state and the SQLite persistent store, so it asks PFIncrementalStore to solve it. PFIncrementalStore just fetch the data from Parse and overwrite the changes. So: the first operation that completes just wins.
If I am correct, maybe a solution could be to wait to propagate saves to the SQLite persistent store for the remote sync to complete.
I should have mentioned the fact that my example project works on OSX (not iOS), and the fetches on Parse are driven by Cocoa bindings (through NSController
s subclasses). At first I thought it was because of the bindings, but I would tend to exclude it now.
Hey @andreacremaschi, thanks for using PFIS! Your explanation and thorough walkthrough seem to pinpoint a bug, but I'll investigate and report back. I was also working on an OS X demo app as part of PR #14, care to push the app on a new branch so I can take a look? It'll provide an example and help with the debugging!