为什么awakeFromInsert调用两次? [英] Why is awakeFromInsert called twice?
问题描述
我已经实现 awakeFromInsert
在我的核心数据对象中设置一些默认值和关系。然而,该方法被调用两次,这意味着我添加的多对值被多次添加。
I have implemented awakeFromInsert
to set some default values and relationships in my core data objects. However, the method is being called twice, meaning that the to-many values I am adding are being added multiple times.
我使用父级和子级托管对象上下文。
I am using parent and child managed object contexts.
推荐答案
awakeFromInsert
上下文。当这个上下文被保存并且更改被推送到父上下文时,它将被再次调用。
awakeFromInsert
will be called when you insert the object into its initial context. When this context is saved and the changes are pushed up to the parent context, it will be called again.
您可以查询 self.managedObjectContext
属性来确定调用该方法的情况。根据您的特定用例,您可能需要检查 parentContext
的存在或不存在,并据此采取行动。
You can query the self.managedObjectContext
property to determine which case the method is being called for. Depending on your particular use case, you may want to check for the presence or absence of a parentContext
and act accordingly.
这篇关于为什么awakeFromInsert调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!