ios 10 NSfetchedResultController does not animate when deleting multiple rows

给你一囗甜甜゛ 提交于 2019-12-24 09:39:32

问题


Hi I'm working with an app with CoreData and NSfetchedResultController. I'm running into a strange issue when deleting multiple rows. The deletion animation work fine with IOS 9 but when come to IOS 10. There is now animation (cell only disappear no mater what animation is choosen).

Here my fetchedResultController (from Master-detail template)

// MARK: - Fetched results controller

var fetchedResultsController: NSFetchedResultsController<Event> {
    if _fetchedResultsController != nil {
        return _fetchedResultsController!
    }

    let fetchRequest: NSFetchRequest<Event> = Event.fetchRequest()

    // Set the batch size to a suitable number.
    fetchRequest.fetchBatchSize = 20

    // Edit the sort key as appropriate.
    let sortDescriptor = NSSortDescriptor(key: "timestamp", ascending: false)

    fetchRequest.sortDescriptors = [sortDescriptor]

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")
    aFetchedResultsController.delegate = self
    _fetchedResultsController = aFetchedResultsController

    do {
        try _fetchedResultsController!.performFetch()
    } catch {
         // Replace this implementation with code to handle the error appropriately.
         // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
         let nserror = error as NSError
         fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
    }

    return _fetchedResultsController!
}    
var _fetchedResultsController: NSFetchedResultsController<Event>? = nil

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    self.tableView.beginUpdates()
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
    switch type {
        case .insert:
            self.tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
        case .delete:
            self.tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
        default:
            return
    }
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
    switch type {
        case .insert:
            tableView.insertRows(at: [newIndexPath!], with: .fade)
            tableView.scrollToRow(at: newIndexPath!, at: .none, animated: true)
        case .delete:
            tableView.deleteRows(at: [indexPath!], with: .right)
        case .update:
            self.configureCell(tableView.cellForRow(at: indexPath!)!, withEvent: anObject as! Event)
        case .move:
            tableView.moveRow(at: indexPath!, to: newIndexPath!)
    }
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    self.tableView.endUpdates()
}

My delete function:

func deleteObjects(_ sender: Any?) {
        NSLog("test")

        let context = self.fetchedResultsController.managedObjectContext

        if (tableView.indexPathsForSelectedRows != nil) {

            let alert = UIAlertController(title: "Confirm delete?", message: "are your sure?", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { okAction in
                for selectedIndex in self.tableView.indexPathsForSelectedRows! {

                    context.delete(self.fetchedResultsController.object(at: selectedIndex))
                }

                do {
                    try context.save()

                } catch {
                    // Replace this implementation with code to handle the error appropriately.
                    // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    let nserror = error as NSError
                    fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
                }
            }))

            alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))

            self.present(alert, animated: true, completion: nil)

        }else{
            let alert = UIAlertController(title: "Select rows to delete", message: "please select at least a row", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

            self.present(alert, animated: true, completion: nil)

        }

Anyway to bring back the animation on IOS 10? anyhelp is much appreciate. THanks!

来源:https://stackoverflow.com/questions/39696524/ios-10-nsfetchedresultcontroller-does-not-animate-when-deleting-multiple-rows

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