Re: CoreData Application not seeing up-to-date information after store updated
Re: CoreData Application not seeing up-to-date information after store updated
- Subject: Re: CoreData Application not seeing up-to-date information after store updated
- From: Andrew Kimpton <email@hidden>
- Date: Wed, 14 Nov 2007 23:06:39 -0500
On Nov 13, 2007, at 11:56 PM, Ben Trumbull wrote:
The piece that isn't apparent (although pretty such I've covered
this in the archives) is the caching that goes on beneath the
NSPersistentStoreCoordinator.
MOC <<-> PSC <<-> Application <<->> Database
-refreshObject:mergeChanges: basically clears/refresh at the level
of the MOC. If the PSC has some of the data you care about cached,
then when the data is viewed again, you'll still see the old cached
data.
For your scenario, the easiest thing to do is, before refreshing,
execute a fetch request to grab the parent and set the keypaths to
prefetch the children. Fetch requests always bring the most recent
data from the database into memory (cached within the PSC). This
can be expensive, as it means a fetch request always performs I/O.
Mitigating the expense is the cache used by faulting, and that's
(probably) why you're seeing stale data.
You can also use the MOC's staleness interval to solve this problem
more broadly to prevent cached data from being reused after a
certain length of time.
I found the stalenessInterval in the docs shortly before getting your
message and tried it out by setting it to something really small
before I do any fetches and then resetting it back again after works
fine. However this seems like rather large hammer to crack a small
nut so I switched and investigated the
setRelationshipKeyPathsForPrefetching method. What I found was that I
had to do the following :
NSFetchRequest *aFetchRequest = [parentArrayController
defaultFetchRequest];
[aFetchRequest setRelationshipKeyPathsForPrefetching:[NSArray
arrayWithObject:@"children"]];
NSArray *array = [[[NSApp delegate] managedObjectContext]
executeFetchRequest:aFetchRequest error:&error];
for (Parent *aParent in array)
{
[[[NSApp delegate] managedObjectContext]
refreshObject:aParent mergeChanges:YES];
}
For things to work out correctly such that a subsequent fetch on the
parent objects gives back a valid set for the children relationship.
Does this still seem appropriate ? This code is executed when the App
is notified that the store has been updated. It's not easy to execute
something like this before the update occurs - but does that really
matter ?
Andrew 8-)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden