问题描述
我正在制作一个从服务器获取产品列表的应用程序。然后将它们存储在Core Data数据库中,并使用呈现它们,并且数据源是NSFetchedResultsController。当我更改服务器中的产品详细信息时,我希望我的iOS应用程序同步并进行必要的更改,因此我实现NSFetchedResultsControllerDelegate方法。如何正确更新gridView?
I'm making an app which fetches a list of products from a server. I then store them in a Core Data database and I present them using GMGridView and the datasource is a NSFetchedResultsController. When I change the product details in the server, I want my iOS app to synchronize and make the necessary changes so I implement the NSFetchedResultsControllerDelegate method. How should I update my gridView properly?
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[_currentData insertObject:anObject atIndex:newIndexPath.row];
[_currentData removeObjectAtIndex:indexPath.row];
[_gmGridView insertObjectAtIndex:newIndexPath.row animated:YES];
[_gmGridView removeObjectAtIndex:indexPath.row animated:YES];
[_gmGridView reloadObjectAtIndex:newIndexPath.row animated:YES];
[_gmGridView reloadObjectAtIndex:indexPath.row animated:YES];
[_gmGridView reloadData];
//[self updatePageControl];
break;
case NSFetchedResultsChangeDelete:
[_currentData removeObjectAtIndex:indexPath.row];
[_gmGridView removeObjectAtIndex:indexPath.row animated:YES];
//[self updatePageControl];
[_gmGridView reloadInputViews];
[_gmGridView reloadData];
break;
case NSFetchedResultsChangeUpdate:
[_gmGridView reloadObjectAtIndex:indexPath.row animated:YES];
////////might be irrelevant, but just trying it out
[_currentData insertObject:anObject atIndex:newIndexPath.row];
[_currentData removeObjectAtIndex:indexPath.row];
[_gmGridView insertObjectAtIndex:newIndexPath.row animated:YES];
[_gmGridView removeObjectAtIndex:indexPath.row animated:YES];
[_gmGridView reloadObjectAtIndex:newIndexPath.row animated:YES];
[_gmGridView reloadObjectAtIndex:indexPath.row animated:YES];
////////
[_gmGridView reloadInputViews];
[_gmGridView reloadData];
break;
case NSFetchedResultsChangeMove:
[_currentData removeObjectAtIndex:indexPath.row];
[_currentData insertObject:anObject atIndex:newIndexPath.row];
[_gmGridView removeObjectAtIndex:indexPath.row animated:YES];
[_gmGridView insertObjectAtIndex:newIndexPath.row animated:YES];
[_gmGridView reloadInputViews];
[_gmGridView reloadData];
break;
}
}
一些详细信息:
_currentData = [[self.fetchedResultsController fetchedObjects]mutableCopy];
//I did this because previously I wasn't using a fetchedResultsController but a NSMutableArray instead. I know that it's inefficient (because I have 2 models) but this is the simplest implementation I want to do now.
我通过修改相同的UIManagedDocument alloc来修改不同类中的CoreData,相同的本地网址。
I make the CoreData changes in a different class by modifying the same UIManagedDocument alloc,init-ed from the same local URL.
但是,我遇到了两个重要问题:
However, I get 2 important problems:
- 更新数据库中的项目(例如更改产品名称或价格),但更改不会反映在UI中,即我无法正确重新加载我的GMGridViewCell。 (注意与上面的重新加载相关的代码)
- 大多数时候,我在服务器上更新的产品在数据库中复制,虽然我有一个机制来防止这样的错误。 (在创建产品之前,我首先使用唯一标识符搜索现有产品,如果有现有产品,我只是修改其详细信息)。以下是代码:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Product"];
request.predicate = [NSPredicate predicateWithFormat:@"product_id = %@", [imonggoInfo objectForKey:PRODUCT_ID]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
request.sortDescriptors = [NSArray
arrayWithObject:sortDescriptor]; NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if (!matches | ([matches count] > 1)){
//handle error
}else if ([matches count] == 0){
//make a new product
}else{
//return existing
item = [matches lastObject];
}
推荐答案
到 NSManagedObjectContextDidSaveNotification
,我必须合并在后台线程中修改的上下文中的更改和主线程中的更改。我从这个中选择的答案得到了想法
Turns out I have to listen to NSManagedObjectContextDidSaveNotification
and I have to merge the changes in the context modified in the background thread and the one in the main thread. I got the idea from the chosen answer in this question
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextChanged:) name:NSManagedObjectContextDidSaveNotification object:nil];
//上下文中的更改必须合并
//Then the changes in the context has to be merged
- (void)contextDidSave:(NSNotification*)notification{
NSLog(@"contextDidSave Notification fired.");
SEL selector = @selector(mergeChangesFromContextDidSaveNotification:);
[self.itemDatabase.managedObjectContext performSelectorOnMainThread:selector withObject:notification waitUntilDone:NO];
}
这篇关于NSFetchedResultsController数据未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!