• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 00:15:00 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

NSPredicate *predicate = [NSPredicate predicateWithFormat"SELF.conversation = %@", self.conversation];

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

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

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

代码下方:

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController) {
        NSLog(@"_fetchedResultsController found: %@", _fetchedResultsController);
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName"ACMessage"];
    NSError __autoreleasing *error = nil;
    NSUInteger messagesCount = [managedObjectContext countForFetchRequest:fetchRequest error:&error];
    NSAssert(messagesCount != NSNotFound, @"-[NSManagedObjectContext countForFetchRequest:error:] error:\n\n%@", error);
    if (messagesCount > MESSAGE_COUNT_LIMIT) {
        [fetchRequest setFetchOffset:messagesCount-MESSAGE_COUNT_LIMIT];
    }
    [fetchRequest setFetchBatchSize:10];
    [fetchRequest setSortDescriptors[[[NSSortDescriptor alloc] initWithKey"sentDate" ascending:YES]]];

    /*
    NSPredicate *predicate = [NSPredicate predicateWithFormat"SELF.conversation = %@", self.conversation];
    [fetchRequest setPredicate:predicate];
    //NSLog(@"redicate: %@", predicate);
    */

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName"ACMessage"];

    _fetchedResultsController.delegate = self;

    NSAssert([_fetchedResultsController performFetch:&error], @"-[NSFetchedResultsController performFetch:] error:\n\n%@", error);
    return _fetchedResultsController;
}

- (void) setSimpleData
{
    self.conversation = [NSEntityDescription insertNewObjectForEntityForName"ACConversation" inManagedObjectContext:managedObjectContext];
    self.conversation.lastMessageSentDate = [NSDate date];
    ACUser *user = [NSEntityDescription insertNewObjectForEntityForName"ACUser" inManagedObjectContext:managedObjectContext];
    [user setName: @"my user"];
    [user setElementId: @"kjh123k123"];

    [self.conversation addUsersObject:user];

    ACMessage *message = [NSEntityDescription insertNewObjectForEntityForName"ACMessage" inManagedObjectContext:managedObjectContext];
    self.conversation.lastMessageSentDate = message.sentDate = [NSDate date];
    self.conversation.lastMessageText = message.text = @"Test message to Fetch through relationship!!!";
    [self.conversation addMessagesObject:message];

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

    NSError *error;
    if (![[self managedObjectContext] save:&error]) {
        //Make sure you handle this!
        NSLog(@"ERROR: Can't save object context");
        exit(-1);
    }
}

- (void) getSimpleData
{
    NSError *error;

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

    NSInteger section = 0;
    id  sectionInfo = [[[self fetchedResultsController] sections] objectAtIndex:section];

    if ([sectionInfo numberOfObjects]>0) {

        NSIndexPath *my0 = [NSIndexPath indexPathForRow:0 inSection:0];
        ACMessage *message = [self.fetchedResultsController objectAtIndexPath:my0];

        ACConversation *conv = message.conversation;

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

    } else {
        NSLog(@"No messages found!");
    }
}

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

谢谢! 亚历克斯



Best Answer-推荐答案


当你第一次创建一个托管对象时,它有一个临时的objectID。当您保存更改时,它会更改为永久的 objectID。这是它唯一一次改变——从最初的临时值变为后来的永久值。您看到的差异正是发生这种变化时的预期结果。

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

BOOL hasTempID = [[myManagedObject objectID] isTemporaryID];

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

NSError *error = nil;
BOOL success = [managedObjectContext obtainPermanentIDsForObjects[myManagedObject] error:&error];

关于ios - 托管对象在获取时具有不同的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14115646/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap