【发布时间】:2015-02-05 17:54:22
【问题描述】:
NSFetchedResultsController 有一些严重的问题,它有一个谓词。更新联系人属性的值后会出现此问题。这就是它的配置方式:
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Contacts" inManagedObjectContext:_addressbookMainObjectContext
NSSortDescriptor *sortNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastnameFirstLetter" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *sortNameDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortNameDescriptor, sortNameDescriptor1, nil];
NSFetchRequest *fetchRequest = [NSFetchRequest new];
[fetchRequest setEntity:entityDescription];
[fetchRequest setSortDescriptors:sortDescriptors];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"contactType == 3"];;
NSFetchedResultsController *fetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:_addressbookMainObjectContext
sectionNameKeyPath:@"sectiononIdentifier"
cacheName:nil];
_fetchResultController = fetchedResultsController;
_fetchResultController.delegate = self;
谓词设置为获取过滤的行。
这就是父子上下文的配置方式:
_addressbookMainObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_addressbookMainObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
_addressbookMainObjectContext setParentContext:_writerContext];
_writerContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_writerContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
[_writerContext setPersistentStoreCoordinator:_persistentStoreCoordinator];
为了更新一行,使用另一个私有上下文来完成这项工作:
NSManagedObjectContext *ctx = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[ctx setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
[ctx setParentContext:[PalDataCache sharedInstance].addressbookMainObjectContext];
[ctx performBlockAndWait:^{
NSError *errorAllCons = nil;
NSArray *allCons = [context executeFetchRequest:allContacts error:&errorAllCons];
Contacts * contact = [allCons objectAtIndex:0];
contact.pictureUpdatedForRedownload = [NSNumber numberWithBool:YES];
}];
所以在这个临时上下文中保存更改后。 NSFetchedResultsControllerDelegate 被触发:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type)
{
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
{
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
case NSFetchedResultsChangeUpdate:
[self configureCell:(contactCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
}
这会触发NSFetchedResultsChangeDelete,这会导致该行从表中删除,但不会从数据库中删除。重新获取后一切都回到了那里。这是一个非常糟糕的用户体验。谓词是根据用户定义的过滤器设置的。如果 fetch 请求中没有谓词,则不会发生此问题。我究竟做错了什么?这种行为有解决办法吗?
编辑: 当我尝试更新联系人的主要上下文中的记录时,仍然会出现此问题。所以它是主要上下文和编写上下文的组合。
【问题讨论】:
-
我没有深入了解您的实际问题,但是您知道,合并策略对子上下文绝对没有任何作用。它们的更改在保存(并因此合并到父级)时,简单地胜过父级上下文中的任何冲突更改。