在从CoreData删除Entry后如何处理包含CoreData Entry的NSMutableArray,显示为“< fault>”。 [英] how to deal with NSMutableArray containing CoreData Entry after Entry is deleted from CoreData, showing as "<fault>"

查看:78
本文介绍了在从CoreData删除Entry后如何处理包含CoreData Entry的NSMutableArray,显示为“< fault>”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PersonsArray:NSMutableArray =

PersonsArray: NSMutableArray =

(
"<null>",
"<null>",
"<null>",
"<MyProject.Person: 0x7ffc5257d850> (entity: Person; id: 0xd000000000040000 <x-coredata://8DD0B78C-C624-4808-9231-1CB419EF8B50/Person/p1> ; data: {\n    image = nil;\n    name = dustin;\n})",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>")

如果用户删除CoreData条目(实体:Person;名称= ustin)

if the user deletes CoreData Entry (entity: Person; name = dustin)

PersonsArray:NSMutableArray =

PersonsArray: NSMutableArray =

(
"<null>",
"<null>",
"<null>",
"<MyProject.Person: 0x7ffc5257d850> (entity: Person; id: 0xd000000000040000 <x-coredata://8DD0B78C-C624-4808-9231-1CB419EF8B50/Person/p1> ; data: <fault>)",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>")

如何检查 PersonsArray 的索引槽是否包含此< fault> 我可以将其返回给< null>

How can I check if a index slot of PersonsArray contains this "<fault>" so that I can return it to "<null>"?

我的代码删除了tableView中的条目(第二个VC)

My code that deletes entry in tableView (second VC)

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    switch editingStyle {
        case .Delete:
            appDel = UIApplication.sharedApplication().delegate as! AppDelegate
            context = appDel.managedObjectContext!

            //Could I do something like give VC2 the PersonsArray and here...
            //ADD Something like
            for ObjectIndex in 0..<PersonsArray.count {
                 if PersonsArray[ObjectIndex] === results[indexPath.row] { 
                     PersonsArray[ObjectIndex] = NSNull()
                 }
            }
            // then continue with the delete?

            context.deleteObject(results[indexPath.row] as! NSManagedObject)
            context.save(nil)
            self.viewDidLoad()
    default:
        return
    }
}


推荐答案

确实没有简单的方法。正确的答案是...不要将 NSManagedObject 实例存储在数组中。在这种情况下,请使用NSFetchedResultsController等,或重新获取对象以确定剩余的内容。

There really is no easy way. The right answer is ... don't store NSManagedObject instances in an array. Use NSFetchedResultsController, etc. when you are in that situation or refetch the objects to determine what is left.

另一种选择是通过侦听<$ c来手动维护数组$ c> NSManagedObjectContextDidSave 通知,并在从上下文中删除对象时将其从数组中删除。

Another option is to manually maintain the array by listening for NSManagedObjectContextDidSave notifications and removing objects from the array when they are deleted from the context.

使用 NSFetchedResultsController 通常更容易。

在主视图控制器中,您可以保留对 -objectID 的引用,而不是对象。然后,当您尝试实现该对象时,如果 nil 不存在,您将得到它。

In your main view controller you can hold a reference to the -objectID instead of the object. Then when you attempt to realize the object you will get back a nil if it is gone. Not pretty but that can work.

但是,您的主视图控制器应该监听 NSManagedObjectContextDidSaveNotification ,而在其中通知是 -userInfo ,该属性内是三个 NSSet 实例。一个包含所有已更新的对象,一个包含所有已插入和删除的对象。然后,您可以测试以查看是否已删除任何数组中的任何内容并将其从数组中删除。

Instead, however, your main view controller should listen for NSManagedObjectContextDidSaveNotification and in that notification is a -userInfo and inside of that property are three NSSet instances. One containing all objects that were updated, one for all that were inserted and for all that were deleted. You can then test to see if anything that was deleted was in your array and remove them from your array.

这篇关于在从CoreData删除Entry后如何处理包含CoreData Entry的NSMutableArray,显示为“&lt; fault&gt;”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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