6

I have two managed object contexts, A and B. These two contexts are kept in sync by merging changes from one to the other whenever they're saved (by monitoring NSManagedObjectContextDidSaveNotification). I've verified that changes to A and B are merging properly.

I have an NSFetchedResultsController monitoring updates to B. I find that certain updates to A do not result in the nsfrc firing on B. When a new managed object is created and added to A with the attributes the nsfrc is looking for (on B), the nsfrc fires after B merges changes from the save of A. However, if the managed object already exists on A (and B), and I modify the object and save A, while I can see that B is merging the changes from the save notification of A, the nsfrc monitoring B does not fire.

What am I missing here?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
thrusty
  • 864
  • 1
  • 8
  • 18
  • Let me clarify that the predicate that the NSFetchedResultsController is searching for is a simple boolean attribute "marked == YES". If the nsfrc is monitoring managed object context A, it fires on these updates always. If it's monitoring B, it only sees objects added to A, not objects modified in A. – thrusty Jun 02 '11 at 16:50
  • Is the predicate run against the `B` entity or the `A` entity? – TechZen Jun 29 '11 at 18:23

2 Answers2

1

I had this problem and it seems that I've solved it. I don't know what I did exactly. I mean I did a couple of things and don't know, which one became the solution. So I will simply describe...

I've refactored my code to follow some "hints", found in official docs & some forums:

  1. If you manage NSManagedObjectContext in another thread, it should be created in that thread and not somehow passed there.
  2. You should add observer (to get save notifications for merging) in main thread only - this way they will be also merged in the main thread.
  3. (Optional, but I did it) You can try to save context in main thread only even if it was created and managed in the background thread.

1 & 3 looks more like some "magic", so I think you should try to follow 2 first. Hope this will help you.

kpower
  • 3,871
  • 4
  • 42
  • 62
  • I've since refactored my code and while this isn't the complete answer, it appears that threading issues were the root cause. – thrusty Sep 02 '11 at 16:18
0

Not sure if this would solve your problem, but try calling processPendingChanges on context B, after changes from A are being merged.

Also, does your nsfrc use caching? Try disabling the cache and see if it makes any difference in your case...

octy
  • 6,525
  • 1
  • 28
  • 38
  • Look at my "solution" (if it can be called so) - my `NSFetchedResultsController` uses caching and there are no problems now. So the problem is definitely not in caching. – kpower Jul 05 '11 at 04:15