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 Scrum Master Certification during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSM Certification Workshop in different cities.

Scrum master certification online

CSM certification online
 
Thanks for sharing this informative content.,
Leanpitch provides online training in Scrum Master Certification during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSM Certification Workshop in different cities.
CSM online

CSM online certification
 
Thanks for sharing this informative content.,
Leanpitch provides online training in Scrum Master Certification during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSM Certification Workshop in different cities.

CSM online training

CSM training online
 

Thanks for sharing this informative content.,
Leanpitch provides online training in Scrum Master Certification during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSM Certification Workshop in different cities.

CSM training online

Scrum master training online

 
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

 
Thanks for sharing this informative content.,
Turient is an All-in-one platform for all our teaching needs. If Teaching is your passion ,enabling is ours
Read the Informative blog - 11 Free Teaching Tools for Online Teachers
11 Free Teaching Tools for Online Teachers
Free Teaching Tools

 
aol mail In this digital era, a lot of people, including me like to .
aol mail
 

Post a Comment

Subscribe to Post Comments [Atom]





<< Home

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

Subscribe to Posts [Atom]