0
NSFetchedResultsController
有一个严重问题,它有一个谓词。在为联系人的属性更新值之后发生该问题。所以这就是它的配置:带谓词的NSFetchedResultsController在更新后获取NSFetchedResultsChangeDelete
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
,导致该行获得从表中删除,但它不是从数据库中删除。在复习之后,所有事情都回到了那里。这是一个非常糟糕的用户体验。谓词根据用户定义的过滤器进行设置。如果在提取请求中没有谓词,则问题不会发生。我究竟做错了什么?有没有解决这个问题的方法?
编辑: 当我尝试更新联系人的主要上下文的记录时,仍然会出现此问题。所以它是主要的上下文和写入上下文的组合。
我没有深入了解您的实际问题,但您知道,合并策略对于子上下文没有任何作用。他们的变化在保存(并且因此合并到父代中)时,仅仅胜过父上下文中的任何冲突变化。 – 2015-02-05 18:32:29