【问题标题】:Remove NSEntityDescription Objects from list of Core Data从核心数据列表中删除 NSEntityDescription 对象
【发布时间】:2017-08-14 16:36:29
【问题描述】:

我想从核心数据中删除我所有的 NSEntityDescription 对象并释放内存。 reset 函数对内存没有任何影响

以下是我的代码

-(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]; 理论上将上下文设置为其基本状态,据我了解,它也会释放内存,但它不会并且保持相同的内存计数

【问题讨论】:

  • 所有这些实例仍由您的阵列保留。
  • @TomHarrington 即使我们评论这一行[self.persons addObject:person]; 即使在退出for循环范围和等待块范围后,它仍然占用相同数量的内存

标签: ios objective-c core-data automatic-ref-counting


【解决方案1】:

尝试在块中使用对 self 的弱引用。

在块的内联声明之前创建引用:

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

然后在那里使用weakself 而不是self。这可能会阻止线程释放其内存。

【讨论】:

  • 你误解了这个问题。
猜你喜欢
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多