更新关系后,NSFetchedResultsController从UITableView中删除行

这是我如何设置NEFetchedResultsController

 private func setupOnceFetchedResultsController() { if fetchedResultsController == nil { let context = NSManagedObjectContext.MR_defaultContext() let fetchReguest = NSFetchRequest(entityName: "WLWishlist") let dateDescriptor = NSSortDescriptor(key: "createdAt", ascending: false) fetchReguest.predicate = NSPredicate(format: "ANY users.identifier = %@", String(WLAppSettings.currentUser!.identifier) ) fetchReguest.sortDescriptors = [dateDescriptor] fetchReguest.fetchLimit = 10 fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchReguest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil) fetchedResultsController.delegate = self try! fetchedResultsController.performFetch() tableView.reloadData() } } 

这是我NSFetchedResultsControllerDelegate

 func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { switch type { case .Insert: if let newIndexPath = newIndexPath { tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade) } case .Delete: if let indexPath = indexPath { print("--->>>REMOVED") tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } case .Update: if let indexPath = indexPath { tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None) } case .Move: if let indexPath = indexPath, let newIndexPath = newIndexPath where indexPath != newIndexPath { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade) } } } 

这是我滚动查看的结果:

在这里输入图像说明

当我select最后一个单元格(“ 我的第一个列表 ”)时,我获取另一个对象并将它们分配为所选对象( WLWishlist )的子对象,

一旦我select该单元格,然后按回我有一个结果:

在这里输入图像说明

所以没有最后一个单元格,并且使用DELETE changeType调用了didChangeObject方法。

在另一个控制器中,我有一个方法来parsing和保存一些对象:

 if let itemsInfo = responseObject?["wishlist_items"] as? Array<NSDictionary> { MagicalRecord.saveWithBlock({ context in let wishlist2 = WLWishlist.findWishlistWithIdentifier(Int(wishlist.identifier), inContext: context) if page == 1 { wishlist2!.items = Set() } for itemInfo in itemsInfo { let item = WLItem.findOrUpdateItemWithDictionary(itemInfo, inContext: context) item.wishlist = wishlist2 } print("-------------->>>before removing items") WLItem.removeOrphanedItemsInContext(context) print("-------------->>>after removing items") }, completion: { finished, error in print("-------------->>>saved items") completionBlock(error) }) } 

注意打印日志。

控制台上的输出是:

2015-09-03 15:28:49.825心愿单[94972:1987359]创build新的专用队列上下文:
————– >>>删除项目之前
————– >>>删除项目后
2015-09-03 15:28:49.836心愿单[94972:1987852]→保存在后台线程
2015-09-03 15:28:49.839心愿单[94972:1987856]→在后台线程上保存
— >>> REMOVED
————– >>>保存的项目

请告诉我,什么是错的? 一切都是因为这一行:

 item.wishlist = wishlist. 

当我评论这个,没关系。

从fetchResultsController中删除keypath(带点)为我解决了这个问题。 在我的情况下,就像将groupBy值从“location.name”更改为“location”一样简单,

EDITED原来,这会影响没有sortBy值的对象(那些触发“节返回nil值的节名键path”错误),所以将groupBy值更改为保证可用的问题解决了我的问题