views:

192

answers:

1

In core data for the iPhone, I was getting all sorts of errors trying to save data to a NSManagedObjectContext.

I believe that my issues were all to do with me using a `NSManagedObjectContext' that was being used in multiple threads.

So I wanted to create a new NSManagedObjectContext and try that, but I cannot find the example code to simply create a new instance...

I know its simple, but I would really appreciate any help here.

Note, I have seen this article on the Apple docs: http://developer.apple.com/iphone/library/documentation/cocoa/conceptual/CoreDataUtilityTutorial/Articles/05_createStack.html

But this uses some code that I am not familiar with, like the XMLStore which is not supported on the iPhone, etc...

Thanks

+1  A: 

Hey @Mark

this is the code to create a new context:

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *managedObjectContext = nil;

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator:coordinator];
        [managedObjectContext setUndoManager:nil];
    }
    return [managedObjectContext autorelease];
}

It's simply create a new instance of the context and set the store that you would like to use.

If you have multiple stores, you would go for something like that:

- (NSManagedObjectContext *)managedObjectContextForStore:(NSString *)store {
    NSManagedObjectContext *managedObjectContext = nil;

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinatorForStore:store];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator:coordinator];
        [managedObjectContext setUndoManager:nil];
    }
    return [managedObjectContext autorelease];
}

For more info, please have a look at Apple's Core Data Tutorial for iOS.

Cheers!

vfn
Thanks VFN, although where does `[self persistentStoreCoordinator]` come from? is that inherited from a super class? Im in a utility class with my code, how should I go about getting the persistentStoreCoordinator?
Mark
You need to implement the code for the setup of your persistence store. Have a look at this sample from Apple: http://developer.apple.com/iphone/library/samplecode/TopSongs/Introduction/Intro.html
vfn
Is it safe to use the MOC from the main thread to get the persistentStoreCoordinator? I doubt that it is, but just asking...
Mark
Well, instead of create the MOC on the thread, my opinion is that the context must be created on a central placed and sent (usually AppDelegate) and passed to other objects. Although I can't see any issue on getting the persistentStoreCoordinator from the MOC of other thread. The persistentStoreCoordinator should be thread safe since it's not altered as a MOC would be.
vfn
Its interesting that you say that, because I just saw the TopSongs app and read through how it sets up its MOC, and it seems to do it the same (similar at least) way that Im doing it... but I seem to be randomly getting errors like: `An NSManagedObjectContext cannot delete objects in other contexts`. Which is puzzling to me...
Mark
ah, actually I sorted that out, that was me using the wrong context to try to delete object created in a different context.
Mark
Cool, I'm glad you figured it out! Cheers
vfn