Slee
Slee

Reputation: 28248

stop CoreData caching results even after mergeChangesFromContextDidSaveNotification

I use CoreData extensively in my app for viewing products. All data syncing is done in the background on a separate NSManagedObjectContexts and when syncing of changes is complete I call mergeChangesFromContextDidSaveNotification

This works perfectly 99% of the time. Here is where it is breaking down:

  1. I view a product in my app.
  2. I decide I want to change a price, so I goto to the web server where I control my products and change my price.
  3. I come back to my app, browse away from that product and my app sync's the changes made on the web server to my app.
  4. I go and view that product again and the price is not changed.
  5. I completely exit the app and come back into it and then I can see the price change.

I believe what is happening is CoreData is serving up a cached version of the product and the fresh one is not served until after the app is restarted of I have viewed a bunch of other items.

How can I clear this cache after my mergeChangesFromContextDidSaveNotification has been called?

This is driving me and my clients crazy - anyone know how I can remedy this situation?

Upvotes: 1

Views: 289

Answers (1)

Jody Hagins
Jody Hagins

Reputation: 28409

Well, there could actually be a lot of reasons for this. One of the big advantages of Core Data is its flexibility... which is also one of its big drawbacks.

You have a ton of knobs that can be set on your database that relate to caching and fetching. One of the easiest, though, is if the fetch goes to the database or just to the most recent MOC.

Namely, you should look at these:

- (BOOL)includesPendingChanges
- (BOOL)shouldRefreshRefetchedObjects

Also, make sure you are handling the background update and DidSave notification properly, because the MOC should have merged those changes.

Unfortunately, Core Data has some, let's say, hard to discover, interactions when using multiple MOCs. You must be very careful to follow all the rules.

Upvotes: 2

Related Questions