我正在使用以下代码来检索 Core Data 上属性的最大值
- (NSDate*)latestDateForLocalEntity:(NSString*)entityString
key:(NSString*)key
inManagedObjectContext:(NSManagedObjectContext*)context {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityString];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:key ascending:YES];
request.sortDescriptors = @[sortDescriptor];
NSError *localError;
NSArray *localResults = [context executeFetchRequest:request error:&localError];
id result = localResults.lastObject;
NSDate *latestLocalDate = [result valueForKey:key];
NSLog(@"Latest local date for entity %@ (%@): %@",entityString,key,latestLocalDate);
// fault newly created objects - don`t need to keep them in RAM:
[localResults enumerateObjectsUsingBlock:^(NSManagedObject *managedObject, NSUInteger idx, BOOL *stop) {
[context refreshObject:managedObject mergeChanges:NO];
}];
// even context reset does not help reducing RAM consumption:
[context reset];
localResults = nil;
return latestLocalDate;
代码检索所有实体,按值对它们进行排序并获取最后一个对象以获得最大值。然后它会尝试(重新)错误地释放 RAM: [context refreshObject:managedObject mergeChanges:NO]
。
但是,内存似乎没有被释放(大约 400.000non-object
个对象保存在内存中)。
我的代码尝试对对象进行故障排除并重置托管对象上下文有什么问题?为什么不释放内存?