NSFetchedResultsController调用didChangeObject而不是update [英] NSFetchedResultsController calls didChangeObject delete instead of update

查看:122
本文介绍了NSFetchedResultsController调用didChangeObject而不是update的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码,我通过Magical Record保存模型:

  MagicalRecord.saveWithBlock({(localContext) - > ; void in 
var localNotification = CDNotification.MR_findFirstByAttribute(notificationID,withValue:notification.notificationID,inContext:localContext)as CDNotification
localNotification.readNumber = NSNumber(bool:true)
}

在调用以上代码后调用Delete而不是update:



func controller(controller:NSFetchedResultsController,didChangeObject object:AnyObject,atIndexPath indexPath:NSIndexPath,forChangeType type:NSFetchedResultsChangeType,newIndexPath:NSIndexPath){
switch type {
case NSFetchedResultsChangeType.Insert:
self.tableView.insertRowsAtIndexPaths([newIndexPath],withRowAnimation:.Fade)
case NSFetchedResultsChangeType.Update:
如果let cell = self.tableView.cellForRowAtIndexPath (indexPath){
self.configureCell(cell as TableViewCell,atIndexPath:indexPath,withNotification:object as CDNotification)
}
self.tableView.reloadRowsAtIndexPaths([indexPath],withRowAnimation:.Fade)
case NSFetchedResultsChangeType.Move:
self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation:.Fade)
self.tableView.insertRowsAtIndexPaths([newIndexPath],withRowAnimation:.Fade)
case NSFetchedResultsChangeType.Delete:
self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation:.Fade)
默认值:
return
}
}

这只会发生在我为fetch请求设置谓词时,例如:

  fetchRequest.predicate = NSPredicate(format:user == john)


解决方案

这里发生的是,而不是你期望的 NSFetchedResultsChangeUpdate 更改事件, NSFetchedResultsChangeDelete 后跟 NSFetchedResultsChangeInsert 。您还可以看到与源和目标具有相同indexPath的 NSFetchedResultsChangeMove 。这是 iOS 9的几个测试版中的已知问题 22380191 和其他人。


This is the code, I save the model via Magical Record:

MagicalRecord.saveWithBlock({ (localContext) -> Void in
                    var localNotification = CDNotification.MR_findFirstByAttribute("notificationID", withValue: notification.notificationID, inContext: localContext) as CDNotification
                    localNotification.readNumber = NSNumber(bool: true)
                })

Delete is called instead of update after the code above is called:

func controller(controller: NSFetchedResultsController, didChangeObject object: AnyObject, atIndexPath indexPath: NSIndexPath, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath) {
    switch type {
    case NSFetchedResultsChangeType.Insert:
        self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
    case NSFetchedResultsChangeType.Update:
        if let cell = self.tableView.cellForRowAtIndexPath(indexPath){
            self.configureCell(cell as TableViewCell, atIndexPath: indexPath, withNotification: object as CDNotification)
        }
        self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    case NSFetchedResultsChangeType.Move:
        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
    case NSFetchedResultsChangeType.Delete:
        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    default:
        return
    }
}

This only happens if I set the predicate for fetch request, in example:

fetchRequest.predicate = NSPredicate(format:"user == john")

解决方案

What is happening here is that instead of the NSFetchedResultsChangeUpdate change event you expect, you are getting a NSFetchedResultsChangeDelete followed by NSFetchedResultsChangeInsert. You may also see a NSFetchedResultsChangeMove with the same indexPath as source and destination. This is a known issue in several beta versions of iOS 9 22380191 and others.

这篇关于NSFetchedResultsController调用didChangeObject而不是update的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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