2017-03-22 84 views
2

我想从核心数据中删除所有的NSEntityDescription对象并释放内存。该reset功能没有任何差别的内存从核心数据列表中删除NSEntityDescription对象

下面是我的代码

-(void)generatePersons: (NSManagedObjectContext *)privatecontext{ 
    self.persons = [[NSMutableArray alloc]init]; 

    [privatecontext performBlockAndWait:^{ 
     for(int i = 1; i< self.dataRows.count; i++){ 
      NSArray *HeaderRow = [self.dataRows objectAtIndex:1]; 
      NSArray *dataRow = [self.dataRows objectAtIndex:i]; 

      if (dataRow.count <= HeaderRow.count){ 
       int index = 0; 

       Person *person = (Person *)[NSEntityDescription 
              insertNewObjectForEntityForName:@"Person" 
              inManagedObjectContext:privatecontext]; 



       [self.persons addObject:person]; 


      } 
     } 

     [privatecontext reset]; 

    }]; 
} 

这个代码[privatecontext reset];理论上设定的背景下其基本状态,在我的理解它会释放内存,以及但它不会并保持相同的内存数量

+0

所有这些实例仍然由您的数组保留。 –

+0

@TomHarrington即使我们评论此行''self.persons addObject:person];'即使在退出for循环范围和等待块范围 –

回答

0

尝试在您的块中使用对自身的弱引用。

创建块的inline声明前参考:

__weak __block __typeof(&*self)weakself = self;

然后用weakself代替self那里。这可能会阻止线程释放其内存。

+0

之后仍然占用相同的内存量,您误解了这个问题。 –

相关问题