NSFetchedResultsController remove row from UITableView after update relationships

拥有回忆 提交于 2019-12-13 01:43:57

问题


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()
    }
}

This is my 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:

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:

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)
        })
    }

Pay attention for print logs.

The output on console is:

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

Please, tell me, what is wrong? Everything is because of this line:

item.wishlist = wishlist. 

When I comment this out, it's fine.


回答1:


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"

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



来源:https://stackoverflow.com/questions/32377070/nsfetchedresultscontroller-remove-row-from-uitableview-after-update-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!