NSFetchedResultsController skips updates for federated NSManagedObjectContext

I have two managed entity contexts: A and B. These two contexts are kept in sync, merging changes from one to the other when they are saved (by monitoring NSManagedObjectContextDidSaveNotification ). I checked that the changes in A and B merge properly.

I have NSFetchedResultsController control updates for B. I find that some updates for A do not cause nsfrc to start on B. When a new managed object is created and added in with the attributes that nsfrc is looking for (on B), nsfrc fires after B merges the changes from save A. However, if the managed object already exists in (and B), and I modify the object and save A, while I see that B merges the changes from the notification of saving A, monitoring nsfrc B does not work.

What am I missing here?

+6
source share
2 answers

I had this problem and it looks like I solved it. I do not know what I did for sure. I mean, I did a couple of things and don’t know which one was the solution. So I’ll just describe ...

I reworked my code to follow some β€œhints” found in white papers and on some forums:

  • If you are managing an NSManagedObjectContext in another thread, it must be created in that thread and not passed in there somehow.
  • You should add an observer (to receive save notifications for merging) only in the main stream - this way they will also be merged into the main stream.
  • (Optional, but I did it). You can try to save the context in the main thread, even if it was created and managed in the background thread.

1 and 3 are more like some β€œmagic”, so I think you should try to follow 2 first. Hope this helps you.

+1
source

Not sure if this will solve your problem, but try calling processPendingChanges in context B, after merging the changes from A.

Also, does nsfrc use caching? Try disabling the cache and see if your business matters ...

0
source

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


All Articles