1
votes

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?

1
Is the Core Data Object IDs will always exist in the memory until I stop running my App......... - Paul

1 Answers

1
votes

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.