gpt4 book ai didi

ios - 从核心数据列表中删除 NSEntityDescription 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:51 26 4
gpt4 key购买 nike

我想从核心数据中删除所有 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]; 理论上将上下文设置为其基本状态,据我所知,它也会释放内存,但它不会释放内存并保持相同的内存计数

最佳答案

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

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

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

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

关于ios - 从核心数据列表中删除 NSEntityDescription 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42953761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com