NSFetchedResultsController XCode 7 issue

前端 未结 3 376
青春惊慌失措
青春惊慌失措 2021-02-02 02:13

Xcode 7 beta 6 and and NSFetchedResultsController gave me headache today. If I compile same(with Swift 2 fixes) code with Xcode 6, program works on device and simulators(iOS 7,

相关标签:
3条回答
  • 2021-02-02 02:49

    I have tried several things and I believe this is a bug. As you can see from my log, even though I update existing record, delegate gets Insert Message. Adding conditional check at .Insert fixed my issue. I am not using any move operation in the table. Therefore same kind of conditional operation maybe necessary as well if you are using .Move My Current code is like below

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
        switch type {
        case .Insert:
            //FIXME: iOS 9 Bug!
            if indexPath != newIndexPath {
                tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
           }
        case .Delete:
            tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
        case .Update:
           tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
            // self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
    
        case .Move:
            tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
            tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
        }
    }
    

    I have tried to update single record, multiple records and I haven't seen any crashes yet so far. I am using

    tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
    

    method but

    self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
    

    also worked during my tests.

    0 讨论(0)
  • 2021-02-02 03:13

    I did not see this problem any more when I build upon SDK 9.1. It seems the bug have been fixed. You can try it.

    0 讨论(0)
  • 2021-02-02 03:15

    This seems to be happening to me when running on Sim/Device < iOS9 using Xcode 7 beta 6:

    2015-09-01 11:39:05.683 Diabetes Pal[15038:245613] *** Assertion failure in 
    -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3347.44.2/UITableView.m:1623
    2015-09-01 11:39:14.954 Diabetes Pal[15038:245613] CoreData: error: Serious application error.  
    An exception was caught from the delegate of NSFetchedResultsController during a call to 
    -controllerDidChangeContent:.  Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)
    Diabetes Pal(15038,0x33291d4) malloc: *** mach_vm_map(size=1048576) failed (error code=3)
    *** error: can't allocate region securely
    *** set a breakpoint in malloc_error_break to debug
    

    A possible workaround is to not animate the changes (until this is fixed):

    //MARK: - fetched results controller delegate
    
    func controllerWillChangeContent(controller: NSFetchedResultsController) {
        let ios9 = NSOperatingSystemVersion(majorVersion: 9, minorVersion: 0, patchVersion: 0)
        if NSProcessInfo().isOperatingSystemAtLeastVersion(ios9) {
            tableView?.beginUpdates()
        }
    }
    
    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {        
        let ios9 = NSOperatingSystemVersion(majorVersion: 9, minorVersion: 0, patchVersion: 0)
        if NSProcessInfo().isOperatingSystemAtLeastVersion(ios9) == false {
            return
        } //[... rest of delegate implementation ...]
    }
    
    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    
        let ios9 = NSOperatingSystemVersion(majorVersion: 9, minorVersion: 0, patchVersion: 0)
        if NSProcessInfo().isOperatingSystemAtLeastVersion(ios9) == false {
            return
        }//[... rest of delegate implementation ...] 
    }
    
    func controllerDidChangeContent(controller: NSFetchedResultsController) {
        let ios9 = NSOperatingSystemVersion(majorVersion: 9, minorVersion: 0, patchVersion: 0)
        if NSProcessInfo().isOperatingSystemAtLeastVersion(ios9) == false {
            tableView?.reloadData()
            return
        }
        tableView?.endUpdates()
    }
    
    0 讨论(0)
提交回复
热议问题