Core Data & Threads Redux
Core Data & Threads Redux
- Subject: Core Data & Threads Redux
- From: Jeff LaMarche <email@hidden>
- Date: Mon, 16 Jan 2006 16:11:42 -0500
Okay, I'm trying to come up with a sample application to show the use
of threads with Core Data because it's something that gets asked
about a lot. I'm having a bit of trouble with it, however.
My thread works just fine. I create a new context, add a bunch of new
entities to the new context (made with the same persistent store
coordinator as the main application context), and keep a list of the
object IDs of the inserted entities. After inserting them, I save the
context, and when it's done saving, I call a method on the main
thread, which I pass the array of Object IDs.
Here is that method, which attempts to make the main context aware of
the new objects:
- (void)notifyMainContextOfNewEntries:(NSArray *)newEntries
{
NSManagedObjectContext *context = [self managedObjectContext];
NSEnumerator *e = [newEntries objectEnumerator];
NSManagedObjectID *oneID;
while (oneID = [e nextObject])
{
NSManagedObject *newObject = [context objectWithID:oneID];
[newObject valueForKey:@"dateAdded"]; // Fires a fault
[context refreshObject:newObject mergeChanges:NO];
//[entryController addObject:newObject];
}
}
This doesn't work, unfortunately. If I quit and come back to the
application, the added entries show up, but the code above doesn't
seem to do anything - the array controller that manages all of the
entities in the context doesn't get updated with the new objects from
this. The line that is commented out was added in my experimentation,
and it does adds rows to the array controller, but they are empty
rows, which isn't right.
What am I missing? How do I notify the main thread's context to go
get these new objects out of the context? I tried figuring it out
from the BackgroundFetching sample code, but I'm having trouble with
it - I can't figure out what the retainedWords array is doing.
Neither of the array controllers uses this array in any way, yet
somehow adding the entities to this array seems to be what that
application does after firing a fault in order to get the object to
show up.
Any help or thoughts would be greatly appreciated. Thanks,
Jeff
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden