Objective C - NSManagedObjectContext and NSFetchedResultsController release handling

i need some information about memory management with NSManagedObjectContext objects. I`m programming on an ipad-App and i work with core data objects.

My UIApplicationDelegate controls the NSManagedObjectContext:

- (NSManagedObjectContext *)managedObjectContext {
if (__managedObjectContext != nil)
    return __managedObjectContext;

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
    __managedObjectContext = [[NSManagedObjectContext alloc] init];
    [__managedObjectContext setPersistentStoreCoordinator:coordinator];
return __managedObjectContext;

I`m using instances of the ManagedObjectContext to display my Core Data Objects. like this. example table view controller:

CRMAppDelegate *appDelegate = (CRMAppDelegate*) [[UIApplication sharedApplication] delegate];
self.managedObjectContext = appDelegate.managedObjectContext;

// Initialize NSFetchedResultsController to retrieve data from the database.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext]];

// Sort results ascending by last name, first name.
NSSortDescriptor *sortDescriptorFirstLetterLastName = [[NSSortDescriptor alloc] initWithKey:@"firstLetterOfLastName" ascending:YES];
NSSortDescriptor *sortDescriptorLastName = [[NSSortDescriptor alloc] initWithKey:@"name_last" ascending:YES];
NSSortDescriptor *sortDescriptorFirstName = [[NSSortDescriptor alloc] initWithKey:@"name_first" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorFirstLetterLastName, sortDescriptorLastName, sortDescriptorFirstName, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptorFirstLetterLastName release];
[sortDescriptorFirstName release];
[sortDescriptorLastName release];

if ([self.managedObjectContext countForFetchRequest:fetchRequest error:NULL] == 0) {
    [self reloadContactData];

// Create NSFetched Results controller instance.
fetchedResultsController = [[NSFetchedResultsController alloc]
[fetchRequest release];
self.fetchedResultsController.delegate = self;

// Load data.
NSError *error;
if (![fetchedResultsController performFetch:&error]) {
    // Update to handle the error appropriately.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

Is it ok to instance the NSManagedObjectContext like this? Because, the retainCount of the context is increasing til 10 while i`m testing my APP, even though i'm releasing the the context in "viewDidUnload" and "dealloc".

I`ve tried retain the context in a handler class like this:

- (NSManagedObjectContext *)getManagedObjectContext
NSManagedObjectContext *context = [self.managedObjectContext autorelease];

return context;

Whats the best way to interact with NSManagedObjectContext - objects and instances? Thx and sry for my stuoid question :)


The value returned by retainCount is meaningless. Don't bother looking at it.

Use the Leaks instrument or heapshot analysis to determine if your app is leaking.

Need Your Help

design database relating to time attribute

mysql sql-server database database-design

I want to design a database which is described as follows:

How to visit a pair of elements in an array only once?

javascript arrays performance algorithm matrix

I'm programming physics for a small 2D game, and I need to check every object on the screen against every other object on the screen. That's O(N^2), and I don't really like that.