NSEntityDescription still exist in memory?
Now I have a Core Data entity "AAA", and I use a method to fetch its result:
- (AAA *)result{ NSEntityDescription *Entity = [NSEntityDescription entityForName:@"AAA" inManagedObjectContext:self.managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init]; [fetchRequest setEntity:aaaEntity]; NSError *error = nil; NSArray *fetchRequestResult = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; AAA *aaa = fetchRequestResult.lastObject; return aaa; }
Then I use Xcode Instruments to check the memory status, it shows:
VM:Core Data Object IDs 4.02MB(Live Bytes)
Is the entity still live in the memory?
First of all I would start to say that you should not be worried about memory when you deal with Core Data. Under the hood the framework manages stuff for you. When you retrieve an object, Core Data populate a cache where data are stored in. In this way, further fetches will not hit the disk but the cache only.
Anyway, you could rely on two different APIs to control memory footprint. The first one is [context reset]
. This will clear the entire object graph (that belongs to a specific context) as if you had just created it.
The second one is [context refreshObject:yourManagedObject mergeChanges:NO]
. It allows releasing an object, or turning it into a fault.
Hope it helps.
链接地址: http://www.djcxy.com/p/36114.html