节约大量物体在coredata
问题描述:
我尝试保存coredata许多对象,但得到这个崩溃:节约大量物体在coredata
Communications error: <OS_xpc_error: <error: 0x19b354af0> { count = 1, contents =
"XPCErrorDescription" => <string: 0x19b354e50> { length = 22, contents = "Connection interrupted" }
}>
Message from debugger: Terminated due to memory issue
我用MagicalRecord:
[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext){
for (int i = 0; i < json.count; i++) {
[Product parseWithData:((NSMutableArray *)json)[i]];
}
}];
Product.m
+ (void)parseWithData:(NSDictionary *)dictionary {
NSString *xml_id = [dictionary[@"XML_ID"] isKindOfClass:[NSString class]] ? dictionary[@"XML_ID"] : @"";
Product *product = [Product getProductWithXML_id:xml_id];
if (!product)
product = [Product MR_createEntity];
product.xml_id = xml_id;
product.code = [dictionary[@"Code"] isKindOfClass:[NSString class]] ? dictionary[@"Code"] : @"";
...
}
你能建议我吗,我该如何保存它?
答
这似乎是内存问题。
尝试围绕你for
循环的内部与
autoreleasepool {
...
}
答
你需要你分页获取数据的方式和/或保存。
通过PAGINATE,我的意思是:
- 下载第1000(例如,它取决于内容真的)
- 当其完成后,保存1000你只是去
- 时所完成,获得下一个1000,再次保存,等等。
您需要知道您尝试获取和使用的数字(如果我没记错的话)SetLimit:在解析方法和SetSkip上。跳过跳过X个第一个元素,并且限制是将被下载的项目的最大数量。 通过这种方式,您可以跳过限值为1000的0,然后使用skip + = limit调用该方法,您将获得第二个1000块,依此类推。最后一个块显然会小于1000.
这样做会大幅增加所花费的时间,但这可以在后台无缝完成;但它会传播到足够少的内存需要。
这样做,看看它是否有很大的不同。如果没有,你可以减少到500而不是1000,或者完全改变你的架构;也许你甚至不需要全部的项目吧!