核心数据:手动故障管理对象后未释放内存 [英] Core Data: Memory not released after manually faulting managed objects

查看:59
本文介绍了核心数据:手动故障管理对象后未释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来检索Core Data上属性的最大值

I am using the following code to retrieve the maximum value of an attribute on 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].

The code retrieves all entities, sorts them by value and takes the last object to get the maximum value. It then tries to (re)fault the values in order to release RAM: [context refreshObject:managedObject mergeChanges:NO].

但是,内存似乎没有释放(大约400.000 non-object个对象保留在内存中).

However, memory seems not being released (roughly 400.000 non-objectobjects are kept in memory).

试图重新引用对象并重置托管对象上下文的代码有什么问题? 为什么不释放内存?

What is wrong with my code trying to refault the objects and resetting the managed object context? Why does it not release memory?

推荐答案

如果您只对一个属性的最大值感兴趣,我建议您仅使用一个对象获取一个对象

If you are only interested in the maximum value of one attribute, I would suggest that you fetch only one object with

// Sort descending to that the first object is that with the maximum value.
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:key ascending:NO];
[request setSortDescriptors:@[sortDescriptor]];
[request setFetchLimit:1];

然后,只有一个对象被加载到内存中,而不是该实体的所有对象.

Then only one object is loaded into memory instead of all objects of that entity.

此外,您只能获取您感兴趣的属性 而不是托管对象:

In addition, you can fetch only the attribute that you are interested in instead of the managed object:

[request setResultType:NSDictionaryResultType];
[request setPropertiesToFetch:@[key]];

然后,结果是一个字典数组,并且未加载任何托管对象 完全在上下文中.需要注意的是,结果反映了持久性存储中的当前状态,而不考虑上下文中任何未决的更改,插入或删除.

Then the result is an array of dictionaries and no managed objects are loaded at all into the context. One caveat here is that the result reflects the current state in the persistent store, and does not take into account any pending changes, insertions, or deletions in the context.

这篇关于核心数据:手动故障管理对象后未释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆