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

ios - 托管对象在获取时具有不同的 id


                                            <p><p>我正在使用 coredata 并且有一个我没有得到的概念;我有 2 个具有一对多关系的托管对象 ACConversation 和 ACMessages,因此我正在保存与对话关联的消息并尝试根据以下条件取回它们:</p>

<pre><code>NSPredicate *predicate = ;
</code></pre>

<p>但是与对象关联的 id 是不同的(它似乎更像是一个内存地址)并且使用上述谓词它不会获取任何东西:</p>

<pre><code>2013-01-01 18:32:13.727 CoreDataTest The message to fetch: &lt;NSManagedObject: 0x818f680&gt; (entity: ACMessage; id: 0x8160630 &lt;x-coredata:///ACMessage/t451CC5A3-5972-479C-BC7C-6B91CA4DA23C4&gt; ; data: {
    conversation = &#34;0x818b770 &lt;x-coredata:///ACConversation/t451CC5A3-5972-479C-BC7C-6B91CA4DA23C2&gt;&#34;;
    sentDate = &#34;2013-01-01 23:32:13 +0000&#34;;
    text = &#34;Test message to Fetch through relationship!!!&#34;;
})
2013-01-01 18:32:13.728 CoreDataTest The conversation associated to the message: &lt;NSManagedObject: 0x818b710&gt; (entity: ACConversation; id: 0x818b770 &lt;x-coredata:///ACConversation/t451CC5A3-5972-479C-BC7C-6B91CA4DA23C2&gt; ; data: {
    draft = nil;
    lastMessageSentDate = &#34;2013-01-01 23:32:13 +0000&#34;;
    lastMessageText = &#34;Test message to Fetch through relationship!!!&#34;;
    messages =   (
      &#34;0x8160630 &lt;x-coredata:///ACMessage/t451CC5A3-5972-479C-BC7C-6B91CA4DA23C4&gt;&#34;
    );
    messagesLength = 0;
    unreadMessagesCount = 0;
    users =   (
      &#34;0x818b610 &lt;x-coredata:///ACUser/t451CC5A3-5972-479C-BC7C-6B91CA4DA23C3&gt;&#34;
    );
})

2013-01-01 18:32:13.782 CoreDataTest The conversation found is: &lt;NSManagedObject: 0x835e440&gt; (entity: ACConversation; id: 0x8199860 &lt;x-coredata://6E4B40F2-F7B4-4275-BF6E-349101A1254F/ACConversation/p290&gt; ; data: &lt;fault&gt;)
2013-01-01 18:32:13.783 CoreDataTest The conversation to find is: &lt;NSManagedObject: 0x818b710&gt; (entity: ACConversation; id: 0x835e2e0 &lt;x-coredata://6E4B40F2-F7B4-4275-BF6E-349101A1254F/ACConversation/p296&gt; ; data: {
    draft = nil;
    lastMessageSentDate = &#34;2013-01-01 23:32:13 +0000&#34;;
    lastMessageText = &#34;Test message to Fetch through relationship!!!&#34;;
    messages =   (
      &#34;0x835e6d0 &lt;x-coredata://6E4B40F2-F7B4-4275-BF6E-349101A1254F/ACMessage/p268&gt;&#34;
    );
    messagesLength = 0;
    unreadMessagesCount = 0;
    users =   (
      &#34;0x835ea20 &lt;x-coredata://6E4B40F2-F7B4-4275-BF6E-349101A1254F/ACUser/p296&gt;&#34;
    );
})
</code></pre>

<p>代码下方:</p>

<pre><code>- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController) {
      NSLog(@&#34;_fetchedResultsController found: %@&#34;, _fetchedResultsController);
      return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = ;
    NSError __autoreleasing *error = nil;
    NSUInteger messagesCount = ;
    NSAssert(messagesCount != NSNotFound, @&#34;- error:\n\n%@&#34;, error);
    if (messagesCount &gt; MESSAGE_COUNT_LIMIT) {
      ;
    }
    ;
    initWithKey:@&#34;sentDate&#34; ascending:YES]]];

    /*
    NSPredicate *predicate = ;
    ;
    //NSLog(@&#34;Predicate: %@&#34;, predicate);
    */

    _fetchedResultsController = [ initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@&#34;ACMessage&#34;];

    _fetchedResultsController.delegate = self;

    NSAssert(, @&#34;- error:\n\n%@&#34;, error);
    return _fetchedResultsController;
}

- (void) setSimpleData
{
    self.conversation = ;
    self.conversation.lastMessageSentDate = ;
    ACUser *user = ;
    ;
    ;

    ;

    ACMessage *message = ;
    self.conversation.lastMessageSentDate = message.sentDate = ;
    self.conversation.lastMessageText = message.text = @&#34;Test message to Fetch through relationship!!!&#34;;
    ;

    NSLog(@&#34;The message to fetch: %@&#34;, message);
    //NSLog(@&#34;The user associated to the conversation: %@&#34;, user);
    NSLog(@&#34;The conversation associated to the message: %@&#34;, self.conversation);

    NSError *error;
    if (![ save:&amp;error]) {
      //Make sure you handle this!
      NSLog(@&#34;ERROR: Can&#39;t save object context&#34;);
      exit(-1);
    }
}

- (void) getSimpleData
{
    NSError *error;

    if (![ performFetch:&amp;error]) {
      // Update to handle the error appropriately.
      NSLog(@&#34;Unresolved error %@, %@&#34;, error, );
      exit(-1);// Fail
    }

    NSInteger section = 0;
    idsectionInfo = [[ sections] objectAtIndex:section];

    if (&gt;0) {

      NSIndexPath *my0 = ;
      ACMessage *message = ;

      ACConversation *conv = message.conversation;

      NSLog(@&#34;The message fetch at index 0 is %@&#34;, message);
      NSLog(@&#34;The conversation found is: %@&#34;, conv);
      NSLog(@&#34;The conversation to find is: %@&#34;, self.conversation);

    } else {
      NSLog(@&#34;No messages found!&#34;);
    }
}
</code></pre>

<p>我是否遗漏了什么,或者我必须在 ACConversation 中设置一个 id 并在 NSPredicate 中使用它来查找与对话关联的所有消息?</p>

<p>谢谢!
亚历克斯</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当你第一次创建一个托管对象时,它有一个临时的<code>objectID</code>。当您保存更改时,它会更改为永久的 <code>objectID</code>。这是它唯一一次改变——从最初的临时值变为后来的永久值。您看到的差异正是发生这种变化时的预期结果。</p>

<p>您可以像这样检查对象是否具有临时 ID:</p>

<pre><code>BOOL hasTempID = [ isTemporaryID];
</code></pre>

<p>您还可以强制转换提前发生 - 在您保存更改之前 - 像这样:</p>

<pre><code>NSError *error = nil;
BOOL success = error:&amp;error];
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 托管对象在获取时具有不同的 id,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/14115646/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/14115646/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 托管对象在获取时具有不同的 id