应用程序打开时,UITableView闪存的更改速度非常快
我有一个应用程序打开时,UITableView闪存的更改速度非常快,uitableview,core-data,ios8,nsfetchedresultscontroller,Uitableview,Core Data,Ios8,Nsfetchedresultscontroller,我有一个UITableView,它从核心数据堆栈获取数据。 从功能上来说,这是可以预期的 然而,我注意到,有时当你启动应用程序时,你会看到过去的更改(即在不同的设备上进行的更改)一闪而过。当应用程序安装在新设备上且iCloud数据未完全加载时,也会发生这种情况。根据更改量的不同,再次添加、更改和删除所有表格单元格可能需要一两秒钟的时间。如果用户是患者,则一切正常,几秒钟后显示正确的数据。但是,如果用户与数据交互,由于数据正在更新,数据只是闪过,这可能会导致应用程序崩溃 如何防止合并数据直接显示到
UITableView
,它从核心数据堆栈获取数据。
从功能上来说,这是可以预期的
然而,我注意到,有时当你启动应用程序时,你会看到过去的更改(即在不同的设备上进行的更改)一闪而过。当应用程序安装在新设备上且iCloud数据未完全加载时,也会发生这种情况。根据更改量的不同,再次添加、更改和删除所有表格单元格可能需要一两秒钟的时间。如果用户是患者,则一切正常,几秒钟后显示正确的数据。但是,如果用户与数据交互,由于数据正在更新,数据只是闪过,这可能会导致应用程序崩溃
如何防止合并数据直接显示到UITableView
?
下面是我代码中可能相关的部分
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(persistentStoreDidImportUbiquitousContentChanges:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:[[MyDataModel sharedDataModel] persistentStoreCoordinator]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(persistentStoreDidChange:) name:NSPersistentStoreCoordinatorStoresDidChangeNotification object:[[MyDataModel sharedDataModel] persistentStoreCoordinator]];
...
}
- (void)persistentStoreDidImportUbiquitousContentChanges:(NSNotification *)notification {
dispatch_async(dispatch_get_main_queue(), ^{
NSManagedObjectContext *mainContext = [[MyDataModel sharedDataModel] mainContext];
[mainContext mergeChangesFromContextDidSaveNotification:notification];
[self reloadTableData];
});
}
- (void)persistentStoreDidChange:(NSNotification *)notification {
[self reloadTableData];
}
- (void)loadLists {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[List entityName]];
[fetchRequest setPropertiesToFetch:@[@"name"]];
[fetchRequest setFetchBatchSize:40];
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:@[sortByName]];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[[MyDataModel sharedDataModel] mainContext]
sectionNameKeyPath:nil
cacheName:nil];
_fetchedResultsController.delegate = self;
NSError *fetchRequestError = nil;
if (![_fetchedResultsController performFetch:&fetchRequestError]) {
NSLog(@"Error while fetching lists: %@ %@", fetchRequestError.localizedDescription, fetchRequestError.localizedFailureReason);
}
...
}
- (void)reloadTableData {
[self loadLists];
[_tableView reloadData];
}
提前感谢您可以将获取结果控制器的委托设置为
nil
,通知用户UI暂时不可用,因为它仍然需要接收更新,然后(在经过足够的时间来接收所有核心数据更改后)将委托切换回self
,更新表格并解锁UI。是否有回拨以确定何时完成所有更新?或者我需要猜测吗?顺便说一句:尝试完全删除代理(只是为了看看会发生什么)。表会像以前一样更新persistentstoredidmimportubiquitouscontentchanges显然会为每个记录的每次更新调用。