菜鸟教程小白 发表于 2022-12-12 13:47:00

ios - 已插入但尚未保存的 NSManagedObjects?


                                            <p><p>可以识别(在运行时)<code>NSManagedObjects</code> 已插入到 <code>NSManagedObjectContext</code>(即 <code>inserted == YES</code>)但具有从来没有<a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/index.html#//apple_ref/occ/instm/NSManagedObjectContext/save:" rel="noreferrer noopener nofollow"><code>save</code>d</a>到目前为止的上下文存储。</p>

<p>我最初认为他们的 <code>objectID</code> 可能仍然是 <code>nil</code>,但事实并非如此。</p>

<p><strong>更新</strong> 我进行了一个快速实验,看看检查 <code>temporaryID</code>(参见 <a href="https://stackoverflow.com/a/28011911/1789384" rel="noreferrer noopener nofollow">accepted answer</a> )是否也适用于嵌套 MOC。这表明不可以:保存子 MOC 只会向上推一层(即到父 MOC,但不是到更上一层的持久存储),因此新插入的 MOC 的对象 ID 仍然是临时的。在这种情况下,确定一个 MO 在子 MOC 中是否是新的并且从未保存(甚至没有保存到父 MOC)的一种方法显然是检查 <code> && !</code> 代替。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以使用 <code>temporaryID</code> <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectID_Class/index.html#//apple_ref/occ/instp/NSManagedObjectID/temporaryID" rel="noreferrer noopener nofollow">property</a> <code>NSManagedObjectID</code>:</p>

<blockquote>
<p>YES if the receiver is temporary, otherwise NO. Most object IDs return
NO. New objects inserted into a managed object context are assigned a
temporary ID which is replaced with a permanent one once the object
gets saved to a persistent store.</p>
</blockquote>

<p>示例用法:</p>

<pre><code>if(object.objectID.isTemporaryID) {
//the object has not been saved yet
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 已插入但尚未保存的 NSManagedObjects?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/28011604/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/28011604/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 已插入但尚未保存的 NSManagedObjects?