【问题标题】:NSFetchedResultsController remove row from UITableView after update relationshipsNSFetchedResultsController 在更新关系后从 UITableView 中删除行
【发布时间】:2015-09-03 13:30:30
【问题描述】:

这就是我设置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)
        }
    }
}

这是我向上滚动视图时的结果:

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

一旦我选择了那个单元格,然后按回我就会得到结果:

所以没有最后一个单元格,didChangeObject 方法被调用,changeType 为DELETE

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

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 Wishlist[94972:1987359] 创建了新的私有队列上下文:
-------------->>>在删除项目之前
-------------->>>删除项目后
2015-09-03 15:28:49.836 Wishlist[94972:1987852] → 保存在后台线程
2015-09-03 15:28:49.839 Wishlist[94972:1987856] → 保存在后台线程
--->>>已移除
-------------->>>保存的项目

请告诉我,出了什么问题?一切都是因为这条线:

item.wishlist = wishlist. 

当我把它注释掉时,没关系。

【问题讨论】:

  • 遇到同样的问题.. 运气好了吗?
  • 是的,我知道常见的问题是什么。你能问一个问题,然后我回答吗?请附上一些代码来调试它。我几乎可以确定您的情况可能出了什么问题。在此处向我发送该问题的链接。
  • 简而言之,您的情况是什么问题?

标签: ios swift xcode nsfetchedresultscontroller


【解决方案1】:

从 fetchResultsController 中删除键路径(带点)为我修复了它。就我而言,它就像将 groupBy 值从“location.name”更改为“location”一样简单

已编辑 事实证明,这会影响没有 sortBy 值的对象(那些触发“A section returned nil value for section name key path”错误的对象),因此将 groupBy 值更改为保证可用的值可以解决我的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多