【发布时间】:2014-07-16 09:54:18
【问题描述】:
我有一个非常冗长的核心数据删除任务,其中包含 92k 条奇怪的记录,这需要很长时间才能运行。最初它锁定了 UI 线程,所以我试图将调用放在后台线程上。然而,我在模拟器中注意到的是,当我在应用程序后台运行时,此任务停止运行。
我想知道是否有一个快速调整我可以制作代码,以便一旦应用程序在后台运行 10 分钟或允许它们运行多长时间,我的删除将继续运行。
当我开始上课时,我会创建一个后台队列:
backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, nil);
然后什么时候删除我的对象
- (void)queueForDelete:(FlightRecording *)flight {
NSError *error = nil;
flight.deleteFlagValue = 1;
[managedObjectContext save:&error];
// Remove flights form Internal table dataStructure
[flightList removeObjectAtIndex:[tmpIndexPath row]];
[[self tableView] deleteRowsAtIndexPaths:@[tmpIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//Queue flight for deletion in the background
dispatch_async(backgroundQueue, ^{
NSLog(@"Adding %@ to deletion Queue", flight.getFileName);
[self deleteFlight:flight];
});
[[self tableView] reloadData];
}
此代码确实使 UI 响应更快,但删除我的对象仍需要大约 5 到 10 分钟(在模拟器中)。我希望用户能够在后台运行应用程序并让删除过程仍在运行,因为无论后台/前台如何,它都会对应用程序的响应能力产生影响。
【问题讨论】:
-
只是一个观察 - 因为你打电话给
deleteRowsAtIndexPaths,所以没有必要打电话给reloadData
标签: ios multithreading core-data grand-central-dispatch