更新核心数据(如果存在)或创建新的托管对象。我可以让这个更快吗?
我有以下代码,我想知道是否有任何方法可以使其更快。基本上,我的应用程序从网络下载一些 JSON(大约 4000 条记录),并根据数据更新或创建我的托管对象。目前它非常慢,我知道为什么,但我对核心数据很陌生,所以我想知道是否有什么办法可以让它更快?
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSMutableArray *coreDataArray = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
[request release];
for (NSDictionary *dict in arr) {
NSArray *filtered = [coreDataArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(code == %@)", [dict objectForKey:@"Code"]]];
//NSLog(@"COREDATA ARRAY: %d FILTERED ARRAY: %d CODE: %@ COREDATA FIRST CODE: %@", [coreDataArray count], [filtered count], [dict objectForKey:@"Code"], [[coreDataArray objectAtIndex:0] code]);
if ([filtered count] > 0) {
Company *c = [filtered objectAtIndex:0];
if ([dict objectForKey:@"Defunct"]) {
NSLog(@"DELETED DEFUNCT COMPANY");
[managedObjectContext deleteObject:c];
} else {
[c populateWithJSONDictionary:dict];
}
} else {
Company *c = (Company *)[NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:managedObjectContext];
[c populateWithJSONDictionary:dict];
}
float percent = (float)[arr indexOfObject:dict]/[arr count];
[self performSelectorInBackground:@selector(updateProgressView:) withObject:[NSString stringWithFormat:@"%f",percent]];
}
[coreDataArray release];
非常感谢您提供的任何帮助。
I have the following code, and I was wondering if theres any way to make this faster. Basically my app downloads some JSON (about 4000 records) from the net, and updates or creates my managed objects based on the data. At the moment it's quite slow, and I can see why, but I'm new to core data so I was wondering if there's anything I can do to make it faster?
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSMutableArray *coreDataArray = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
[request release];
for (NSDictionary *dict in arr) {
NSArray *filtered = [coreDataArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(code == %@)", [dict objectForKey:@"Code"]]];
//NSLog(@"COREDATA ARRAY: %d FILTERED ARRAY: %d CODE: %@ COREDATA FIRST CODE: %@", [coreDataArray count], [filtered count], [dict objectForKey:@"Code"], [[coreDataArray objectAtIndex:0] code]);
if ([filtered count] > 0) {
Company *c = [filtered objectAtIndex:0];
if ([dict objectForKey:@"Defunct"]) {
NSLog(@"DELETED DEFUNCT COMPANY");
[managedObjectContext deleteObject:c];
} else {
[c populateWithJSONDictionary:dict];
}
} else {
Company *c = (Company *)[NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:managedObjectContext];
[c populateWithJSONDictionary:dict];
}
float percent = (float)[arr indexOfObject:dict]/[arr count];
[self performSelectorInBackground:@selector(updateProgressView:) withObject:[NSString stringWithFormat:@"%f",percent]];
}
[coreDataArray release];
Many thanks for any help you can give.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该查看核心数据编程指南:性能 部分
它对数据导入性能有一些具体的建议。
如果苹果再次移动文档,这里是 Google
site:developer.apple.com 核心数据导入性能
上的一个很好的搜索查询You should check out the Core Data Programming Guide: Performance section
It has some specific advice for data import performance.
In case Apple moves the documentation again, here is a good search query on Google
site:developer.apple.com core data import performance