【发布时间】:2016-01-10 06:16:08
【问题描述】:
当我从 Internet 下载大数据(使用 url 分页)并将其保存在核心数据中时,我遇到了一个问题。在保存数据期间,我的应用程序 GUI 被冻结。保存数据后,我的应用程序正常工作。我需要您帮助我解决应用程序冻结问题。
我正在使用异步的NSURLConnection 委托方法下载数据。在connectionDidFinishLoading 方法中,我将数据插入到核心数据中。这是connectionDidFinishLoading 方法中的代码。
[Customer insertCustomerWithList:data];
[self.delegate connectPermissionServiceSuccessful:method];
在insertCustomerWithList 上,方法应用程序被冻结。
如果我使用dispatch_async GCD 方法,那么应用程序会因给出此错误而开始崩溃。
ERROR*** 集合 NSCFSet: 0x7c2282e0> 在发生突变时 被枚举。
这是带有 GCD 的代码。
dispatch_queue_t coreDataThread = dispatch_queue_create("com.YourApp.YourThreadName", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(coreDataThread, ^{
[Customer insertCustomerWithList:data];
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate connectPermissionServiceSuccessful:kMethod];
});
});
这是在核心数据中插入数据的insertCustomerWithList 方法。
+ (BOOL) insertCustomerWithList:(NSArray*)customerList
`{NSError *error = nil;
BOOL success = YES;
Customer *obj = nil;
NSManagedObjectContext *managedObjectContext = [NSManagedObjectContext managedObjectContext];`
if (managedObjectContext == nil)
{
success = NO;
}
@try
{
for (NSInteger index = 0; index < customerList.count ; index ++)
{
NSDictionary* customerInfo = customerList[index];
obj = [self getCustomerByID:customerInfo[@"_id"]];
if (obj == nil)
{
obj = [NSEntityDescription insertNewObjectForEntityForName:kEntityName
inManagedObjectContext:managedObjectContext];
[obj updateCustomer:customerInfo];
}
else
{
[obj updateCustomer:customerInfo];
}
}
}
@catch (NSException *exception)
{
NSLog(@"__ERROR__%@__", exception.reason);
}
[managedObjectContext save:&error];
return success;
`}`
请帮助我解决此问题。
【问题讨论】:
-
题外话:你不应该使用异常处理。
标签: ios objective-c cocoa-touch core-data