I’m battling to understand the problem I’m having with Core Data and a simple fetch request:
I need to display some records and I execute these lines of code
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Venue"
inManagedObjectContext:self.managedObjectContext]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"id_" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[sortDescriptor release];
sortDescriptor = nil;
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
sortDescriptors = nil;
[self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
So far so good, but in Instruments I can see that before loading the records, my memory is 9mb, afterwards the memory jumps to 44mb (!!) and still there. But I want to release all the records from memory because I don’t need them anymore. Did I miss something? I thought that Core Data was releasing the records after they aren’t needed anymore. I tried to do a for-cycle to release every ManagedObject, but they are already +1 count, meaning they are soon to be released.
If you no longer need your NSManagedObjects, you can manually turn them back into a
fault. i.e. Remove them from the NSManagedObjectContext. Next time you want them, they will be loaded from the persistent store again.I do this to conserve memory when I’m synchronising with a server and updating objects, but I don’t need to use them immediately.
To re-fault, use this (and read the API Docs about it’s usage)