• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: CoreData - trying to avoid separate thread
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CoreData - trying to avoid separate thread


  • Subject: Re: CoreData - trying to avoid separate thread
  • From: Karl Moskowski <email@hidden>
  • Date: Fri, 5 Jan 2007 00:21:56 -0500

Rich,

I had a similar problem a few weeks back - programmatically loading a CD model from a large third-party XML file, while permitting a responsive UI. Matt Neuberg replied that he did something similar in Thucydides (a Safari bookmarks manager, IIRC), and pointed me at his sample code.
<http://lists.apple.com/archives/Cocoa-dev/2006/Nov/msg00984.html>


FWIW, I came up with something like this:

- (IBAction) beginImport:(id) sender {
// here, you can set up the UI, maybe start a progress indicator
// then, do the actual work of the import on a separate thread
[NSThread detachNewThreadSelector:@selector(doImport) toTarget:self withObject:nil];
}


- (void) doImport {
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];

// create a new local MOC on top of the existing PSC
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
[moc setPersistentStoreCoordinator: [self persistentStoreCoordinator]];

// do the import - read the file (XML), insert objects into the local MOC
MyManagedObject *myMO = [NSEntityDescription insertNewObjectForEntityForName:@"MyManagedObject"
inManagedObjectContext:moc];
[myMO setMyFoo:@"Bar"];
// etc.


// at various points, you could performSelectorOnMainThread: to update the progress if you need to
// or check a cancellation flag then bail out

// save the local MOC, then get the IDs of all the registered objects (KVC is cool)
// and pass them back to the completion method on the main thread
NSError *error = nil;
[moc save:&error];
[self performSelectorOnMainThread:@selector(completeImport:)
withObject:(NSArray *)[[moc registeredObjects] valueForKey:@"objectID"]
waitUntilDone:NO];
[pool release];
}


- (void) completeImport:(NSArray *)objectIDs {
// refresh the inserted objects from the underlying PSC to the main thread's MOC
NSEnumerator *e = [objectIDs objectEnumerator];
NSManagedObjectID *moid;
while (moid = [e nextObject])
[[self managedObjectContext] refreshObject:[moc objectWithID:moid] mergeChanges:YES];

}


--Karl <email@hidden>


On 05/01/2007, at 6:34 AM, Richard Salvatierra wrote:

I am trying to add to a managedContext during the import of files.
When I do this in a separate thread, there are unexpected behaviors
(display pattern does not update).
I am trying to avoid using a separate thread, but I do want the
user to be able to cancel the import while in process.  If I am in
a while loop (while files are importing) how can I check for user
input (i.e. mousedown on cancel button)
-Rich
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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


  • Prev by Date: Re: Getting a list of files/folders in a folder/drive
  • Next by Date: Re: Help with Predicate count/relationships
  • Previous by thread: Re: CoreData - trying to avoid separate thread
  • Next by thread: NSTextView + NSTabView - Frame/Scroll issues?
  • Index(es):
    • Date
    • Thread