Core Data Nullify何时更新规则? [英] When does Core Data Nullify rule update relations?

查看:141
本文介绍了Core Data Nullify何时更新规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对多的关系A< --- >> B(订购多对象部分)。

I have a one-to-many relationship A <--->> B (the to-many part is ordered).


  • 当删除A时,与A有关系的所有B也应该被删除,因此A与B的关系的删除规则是设置为级联 - >正常工作

  • 删除B时,只应清除返回A的关系,因此B与A关系的删除规则设置为nullify - >不起作用(仅在延迟之后)

  • When deleting A all B's with a relation to A should be deleted as well, so the deletion rule for A's relation to B is set to cascade -> Works fine
  • When deleting B only the relationship back to A should be cleared, so the deletion rule for B's relation to A is set to nullify -> Doesn't work (only after a delay)

所以我遇到与此问题中所述完全相同的问题核心数据Nullify规则不起作用?:我删除了一个与A有关系的B,然后我立即计算剩余B的数量,A有关系和以前一样。该问题中接受的答案是使用级联而不是nullify,因为nullify的作用是:

So I have the exact same problem as stated in this question "Core Data Nullify rule doesn't work?": I delete a B that has a relation to an A and immediately after that, I count the number of remaining B's, that A has a relation with and it is the same as before. The accepted answer in that question was to use cascade instead of nullify since what nullify does is:


Nullify在对象时将指针设置为null被删除。如果
有一个指针数组,它不会删除它,它只是将它设置为
null。

Nullify sets the pointer to null when the object is deleted. If you have an array of pointers it doesn't remove it, it just sets it to null.

我看到答案有2个问题:

I see 2 issues with that answer:


  1. 我很确定在这种情况下cascade是错误的规则,因为它也会删除删除B时的A,这不是我想要实现的。 (我尝试了它,结果是我的预期:A也被删除了。)

  2. 除了一个使用NSNull单例之外,集合不能将null作为其元素之一。所以我怀疑这就是无效规则的作用。

经过实验后我发现,如果我删除了一个实例B它会被立即删除,但是与A的关系不会立即被清除,但只是在一点延迟之后:

After experimenting a bit I found out that, if I delete an instance of B it is deleted immediately but the relation to A is not cleared immediately but only after a little delay:

// Method is called by pressing a button
-(void)removeLastBOfA:(A *)instanceOfA
{
    // Prints 4
    NSLog(@"fetch all b's count before:%d", [context fetchAllBs].count);
    // Prints 4
    NSLog(@"A's relation to B count before: %d", instanceOfA.relationToB.count);

    [context deleteObject:[instanceOfA.relationToB lastObject]];

    // Prints 3
    NSLog(@"fetch all b's count after:%d", [context fetchAllBs].count);
    // Prints 4, but should be 3. Last Object of A's relationToB is still the object that was deleted
    NSLog(@"A's relation to B count after: %d", instanceOfA.relationToB.count);

}

现在按下按钮再次调用上面的方法而不做任何介于两者之间的任何事情,突然关系更新并打印出A与B计数之前的关系: 3 。因此,nullify删除规则确实可以正常工作,但有一点延迟。

Now when pressing the button to call the method above again without doing anything in between, suddenly the relation is updated and "A's relation to B count before: 3" is printed. So the nullify deletion rule does work as I want it to, but with a little delay.


  1. 我说的2个问题是否有效?

  2. 为什么nullify只会在延迟后更新关系,那是什么延迟?或者在删除NSManagedObject后关系会在哪个时刻更新?


推荐答案

是的你是对。答案是不正确的。关于第二点。方法-deleteOdject不会删除对象,因为它只是将对象标记为已删除。要完成删除,您需要保存托管对象上下文,然后您将看到nullify规则按预期工作。如果您此时不想保存上下文,可以采用以下两种方式:

Yes you are right. The answer is not correct. Regarding to second point. The method -deleteOdject does not delete object as you expected it just marks object as deleted. To accomplish deletion you need to save managed object context and then you will see that nullify rule works as expected. If you do not want to save context at this moment you could follow two ways:


  1. 明确删除关系:

  1. Explicitly delete relationship:

NSManagedObject* objectToDelete = [instanceOfA.relationToB lastObject];
[context deleteObject:objectToDelete];
NSMutableSet* relationships = [instanceOfA mutableSetValueForKey:@"relationToB"];
[relationships removeObject:objectToDelete];


  • 请求上下文处理其未来的更改(这意味着计算由删除引起的更改):

  • Ask context to process its future changes (it means calculate changes caused by your deletion):

    [context processPendingChanges];
    


  • 在你的例子中:

    [context deleteObject:[instanceOfA.relationToB lastObject]];
    [context processPendingChanges];
    // Prints 3
    NSLog(@"fetch all b's count after:%d", [context fetchAllBs].count);
    NSLog(@"A's relation to B count after: %d", instanceOfA.relationToB.count);
    

    之后你会看到预期的结果。

    After that you will see expected result.

    NSManagedObjectContext在运行循环结束时或执行 -save:时自行执行 -processPendingChanges ,这样就是为什么你会看到一些延迟。

    NSManagedObjectContext does -processPendingChanges by itself at the end of run loop or when performing -save:, so that is why you see some delay.

    希望它有所帮助。

    这篇关于Core Data Nullify何时更新规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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