NSFetchedResultsController 在更新关系后从 UITableView 中删除行 [英] NSFetchedResultsController remove row from UITableView after update relationships

查看:75
本文介绍了NSFetchedResultsController 在更新关系后从 UITableView 中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我设置 NEFetchedResultsController 的方式:

This is how I setup 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)
        }
    }
}

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

And this is the result when I scroll view up:

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

When I select last cell ("My First List"), I fetch another objects and assign them as a child of selected object (WLWishlist)

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

Once I select that cell, and then press back I have a result:

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

So there is no last cell, and didChangeObject method was called with changeType of DELETE.

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

In another controller I have a method to parse and save some objects:

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] → 保存在后台线程
--->>>已删除
-------------->>>保存的项目

2015-09-03 15:28:49.825 Wishlist[94972:1987359] Created new private queue context:
-------------->>>before removing items
-------------->>>after removing items
2015-09-03 15:28:49.836 Wishlist[94972:1987852] → Saving on a background thread
2015-09-03 15:28:49.839 Wishlist[94972:1987856] → Saving on a background thread
--->>>REMOVED
-------------->>>saved items

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

item.wishlist = wishlist. 

当我把这个评论出来时,就没事了.

When I comment this out, it's fine.

推荐答案

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

Removing keypath (with dot) from fetchResultsController fixed it for me. In my case it was as simple as changing groupBy value from "location.name" to "location"

已编辑原来这会影响没有 sortBy 值的对象(那些触发节名称键路径的节返回 nil 值"错误的对象),因此将 groupBy 值更改为保证可用的值修复了我的问题

EDITED Turns out this affects objects that do not have sortBy value (those that trigger "A section returned nil value for section name key path" error), so changing the groupBy value to the one that is guaranteed to be available fixed my problem

这篇关于NSFetchedResultsController 在更新关系后从 UITableView 中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆