简体   繁体   English

删除对象的CoreDataGeneratedAccessors似乎没有被删除

[英]CoreDataGeneratedAccessors to remove object don't seem to be deleting

I have an NSManagedObject that has a to-many relationship to another NSManagedObject. 我有一个与另一个NSManagedObject有多对多关系的NSManagedObject。

During creation of the NSManagedObject I can use the generated accessors 'removeNotesObject' and the deletion works fine. 在创建NSManagedObject期间,我可以使用生成的访问器'removeNotesObject',删除工作正常。 I can create an object to add to the parent object, save the object, delete the object and then save again. 我可以创建一个对象添加到父对象,保存对象,删除对象,然后再次保存。 When I fetch this parent object the object I created and deleted is still deleted. 当我获取此父对象时,我创建和删除的对象仍然被删除。

However, after I add the object and then save it (but don't delete and save after) and then fetch it, I can't seem to delete the object that was previously created. 但是,在我添加对象然后保存它(但不删除并保存之后)然后获取它之后,我似乎无法删除先前创建的对象。 I am using the generated accessors to try and remove the object, which appears to work but when I fetch it again the object hasn't been deleted. 我正在使用生成的访问器来尝试删除对象,这似乎有效,但是当我再次获取它时,该对象尚未被删除。

(Note: Adding objects does work so it is not a problem with the saving) (注意:添加对象确实有效,因此保存时不会出现问题)

To delete the object I retrieve the set of object and select the objects I want to delete. 要删除对象,我将检索对象集并选择要删除的对象。 Then I remove the objects 然后我删除了对象

NSSet *notes = summary.notes;
NSSet *oldNotes = [notes objectsPassingTest:^(id obj,BOOL *stop){
    Note *oldNote = (Note *)obj;
    BOOL sameRow = (oldNote.row == newNote.row);
    BOOL sameColumn = (oldNote.column == newNote.column);
    BOOL success = (sameRow && sameColumn);
    return success;}];
[summary removeNotes:oldNotes];

I have tried making the relationship inverse to delete the objects which didn't delete them. 我已经尝试将关系反转为删除没有删除它们的对象。 I have also tried different delete rules (cascade and nullify) which again didn't work. 我也尝试了不同的删除规则(级联和无效),这再次没有用。 Finally, I tried to remove each object separately and deleting each object from the context after I had removed it from the parent object which again unfortunately didn't work. 最后,我尝试分别删除每个对象,并在从父对象中删除它之后从上下文中删除每个对象,这再次遗憾地不起作用。

I assume the problem must be something to do with it being a fetched object. 我认为问题必须与它作为一个获取的对象有关。 If anyone could help I would really appreciate it as I can't think of any other ways to test or solve this problem. 如果有人可以提供帮助,我会非常感激,因为我无法想到任何其他方法来测试或解决这个问题。

You need to do 你需要这样做

NSManagedObjectContext * moc = .......;
[moc deleteObject:note]

edit: The core data generated accessors simply remove the object from the relationship, but do not delete the object permanently. 编辑:核心数据生成的访问器只是从关系中删除对象,但不会永久删除该对象。 This makes sense because you may have one NSManagedObject associated to multiple other NSManagedObjects via relationships. 这是有道理的,因为您可能有一个NSManagedObject通过关系与多个其他NSManagedObjects相关联。

edit: Deleting in the above mentioned fashion will invoke the deletion rules. 编辑:以上述方式删除将调用删除规则。 I suggest you double check that they are setup correctly. 我建议你仔细检查它们是否设置正确。

The reason the above code did not work is that == will not actually compare the NSNumber. 上面的代码不起作用的原因是==实际上不会比较NSNumber。 Instead you need to call 'isEqualTo:'. 相反,你需要调用'isEqualTo:'。 I think before it was checking the address hence working before I saved it. 我想在我保存它之前检查地址因此工作。 What's more it was returning an object in the NSSet so appeared to be working. 更重要的是它在NSSet中返回一个对象似乎正在工作。 During debugging it wasn't clear what the object was but clearly wasn't the one I needed. 在调试期间,不清楚对象是什么,但显然不是我需要的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM