小编bru*_*sas的帖子

添加到关系后未在coredata中找到对象

我在添加关系后获取对象时遇到问题.我第一次获取类别,总是找到,然后当我添加到关系时,找不到以下类别.

这种关系是一种Many-To-Many.

例:

  • 获取类别 categoryId = 10
  • 找到类别对象
  • 添加到父对象关系
  • 下一个对象
  • 如果几个类别相同id,则categoryId = 10找不到

    NSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    
       [private setParentContext:self.model.context];
        __block NSError *error = nil;
    
        [private performBlockAndWait:^{
    
            GPDeal *deal = [EKManagedObjectMapper objectFromExternalRepresentation:dic withMapping:[GPDeal objectMapping] inManagedObjectContext:private];
            for (NSDictionary *dic in responseObject[@"response"]) {
    
                GPCategory *category;
    
               //The first time always found
                if ((category = [GPCategory MR_findFirstByAttribute:@"catId" withValue:dic[@"mainAttribute"] inContext:private])) {
                    NSLog(@"Found");
                    [category addDealsObject:deal];
    
                } else {
                    NSLog(@"Not Found");
    
                }
    
            }
        }];
    
        NSError *PrivateError = nil;
        if (![private …
    Run Code Online (Sandbox Code Playgroud)

core-data objective-c relationship nsfetchrequest ios

6
推荐指数
1
解决办法
293
查看次数