Monday, January 18, 2010

 

Using Core Data in a multithreaded environment.

You have to do a lot of iPhone stuff in background threads, to maintain a snappily responsive user interface. But if you're using Core Data, well - to quote the docs:

Managed objects are not thread safe [...] Core Data does not present a situation where reads are "safe" but changes are "dangerous"—every operation is "dangerous" because every operation can trigger faulting.

Sound intimidating? No worries: it's a piece of cake. Just wrap the code where you get your ManagedObjectContext in a method something like this:


@implementation Util

+(NSManagedObjectContext*)getManagedObjectContext {
UIApplication *app = [UIApplication sharedApplication];
MyAppDelegate *appDelegate = app.delegate;
if ([NSThread isMainThread])
return [appDelegate managedObjectContext];
else
return [appDelegate getMOCFor:[NSThread currentThread]];
}


and add the following method to your application delegate:
(where "threadMOCs", obviously, is a properly defined and synthesized ivar.)


- (NSManagedObjectContext*) getMOCFor:(NSThread*) thread {
if (!threadMOCs)
self.threadMOCs=[NSMutableDictionary dictionaryWithCapacity:16];

NSNumber *threadHash = [NSNumber numberWithInt:[thread hash]];
if ([threadMOCs objectForKey:threadHash]==nil) {
NSManagedObjectContext *newMOC = [[NSManagedObjectContext alloc] init];
NSError *error=nil;
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"myDBName.sqlite"]];
NSManagedObjectModel *model = [self managedObjectModel];
NSPersistentStoreCoordinator* threadPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: model];
if (![threadPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error])
{ //handle error here }

[newMOC setPersistentStoreCoordinator: threadPSC];
[threadMOCs setObject:newMOC forKey:threadHash];
}
return [threadMOCs objectForKey:threadHash];
}


Et voila! A new object context and persistent store coordinare for every thread, which gives you full concurrent access to your data. (Though you probably still have to be careful about passing managed objects across threads...)

Labels: , , , , , , , , , , ,


Comments:
Thanks for sharing this informative content.,
Leanpitch provides online training in Agile team facilitation during this lockdown period everyone can use it wisely.

Agile team facilitation

ICP ATF

Team facilitator Agile

Agile facilitator

Team facilitator in Agile

ICAGILE ATF

 
Enhance your knowledge of Core Data in a multithreaded environment through our informative resources. Optimize your data management skills with our guidance.
 

Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]