Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly manage the NSManagedObjectContext in each view controller?

I'm relative new with CoreData and I want to know if I'm doing the things right. First the documentation says:

"By convention, you get a context from a view controller. You must implement your application appropriately, though, to follow this pattern.

When you implement a view controller that integrates with Core Data, you can add an NSManagedObjectContext property.

When you create a view controller, you pass it the context it should use. You pass an existing context, or (in a situation where you want the new controller to manage a discrete set of edits) a new context that you create for it. It’s typically the responsibility of the application delegate to create a context to pass to the first view controller that’s displayed."
https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/stack.html

so what I do is create a property for my NSManagedObjectContext:

MyViewController.H
@interface MyViewController : ViewController
{
    NSManagedObjectContext *moc;
}

@property (nonatomic, retain) NSManagedObjectContext *moc;

@end

 MyViewController.m
 @implementation MyViewController
 @synthesize moc=moc;

1.-And any place I want to do some change to the database I do this.

MainNexarAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];

self.moc = [[NSManagedObjectContext alloc] init];
self.moc.persistentStoreCoordinator = [appDelegate persistentStoreCoordinator];
/*code**/
[self.moc save:&error];

2-.And if I'm going to work in a different thread I have my custom method to create the NSManagedObjectContext with NSPrivateQueueConcurrencyType so it can be manage in a private queue:

   //Myclass NSObject<br>

 -(NSManagedObjectContext *)createManagedObjectContext{

    MainNexarAppDelegate *appDelegate =
    [[UIApplication sharedApplication] delegate];

    NSPersistentStoreCoordinator *coordinator = [appDelegate persistentStoreCoordinator];
    if (coordinator != nil) {
        __managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];

        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return __managedObjectContext;  
}
//__managedObjectContext is my property from the .h file 
//@property (readonly,strong,nonatomic)  NSManagedObjectContext* managedObjectContext;
  1. Is a good practice create a NSManagedObjectContext for each view controller where you will do some change to the database?
    1.1. It's a valid approach use [UIApplication sharedApplication] to get the persistent NSPersistentStoreCoordinator form the appdelegate?
  2. It's safe to share the persistent store coordinator between the main thread and any other thread?

Any help will be appreciated :).

like image 327
Goca Avatar asked Oct 01 '13 18:10

Goca


People also ask

What is the purpose of managed object context NSManagedObjectContext?

A managed object context is an instance of NSManagedObjectContext . Its primary responsibility is to manage a collection of managed objects. These managed objects represent an internally consistent view of one or more persistent stores.

What is NSManagedObjectContext in Swift?

An object space to manipulate and track changes to managed objects.

Can we have multiple managed object context in Core Data?

Most apps need just a single managed object context. The default configuration in most Core Data apps is a single managed object context associated with the main queue. Multiple managed object contexts make your apps harder to debug; it's not something you'd use in every app, in every situation.

How does Core Data Work?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.


1 Answers

I am going to disagree with most of the answers here. It is NOT bad for #1. In fact, it is probably good practice in most cases to do so. Especially if you have different threads running stuff. It has greatly simplified my apps to create NSManagedObjectContexts whenever needed including per view controller. This is also recommended by the guys behind MagicalRecord (which is what I use to make use of Core Data in most cases). NSManagedObjectContext creation is not a high overhead call per the MR guys. I am not a CoreData expert by any stretch of the imagination, but I have had much better results in doing it this way, as recommended to me by the MagicalRecord guys.

like image 71
chadbag Avatar answered Oct 11 '22 16:10

chadbag