添加到关系

时间:2015-12-27 18:06:55

标签: ios objective-c core-data relationship nsfetchrequest

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

关系是Many-To-Many

示例:

  • 使用categoryId = 10
  • 获取类别
  • 找到类别对象
  • 添加到父对象关系
  • 下一个对象
  • 如果多个类别具有相同的idcategoryId = 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 save:&PrivateError]) {
            NSLog(@"Unresolved error %@, %@", PrivateError, [PrivateError userInfo]);
            abort();
        }
    
        if (!error) {
            //Save on main moc
            [self.model saveWithErrorBlock:^(BOOL success, NSError *error) {
                if (!success) {
                    NSLog(@"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
                }
            }];
    
        } else {
            NSLog(@"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
        }
    

修改

解决了,我想我的问题是我忘了在每次迭代结束时保存主要上下文。

        NSManagedObjectContext *backgroundMOC = [self.model backgroundMOC:self.model.context];

        [backgroundMOC performBlockAndWait:^{

            for (NSDictionary *dic in responseObject[@"response"]) {

                GPDeal *deal = [EKManagedObjectMapper objectFromExternalRepresentation:dic withMapping:[GPDeal objectMapping] inManagedObjectContext:backgroundMOC];

                GPCategory *category;
                if ((category = [GPCategory MR_findFirstByAttribute:@"catId" withValue:dic[@"mainAttribute"] inContext:backgroundMOC])) {
                    NSLog(@"Found with mainAttribute %@", dic[@"mainAttribute"]);
                    [deal addDealCategoryObject:category];
                }

                if([backgroundMOC hasChanges]) {
                    NSError * error;
                    [backgroundMOC save:&error];

                    [self.model.context performBlockAndWait:^{
                        if([self.model.context hasChanges]) {
                            NSError * error;
                            [self.model.context save:&error];
                        }
                    }];
                }
            }
        }];

1 个答案:

答案 0 :(得分:1)

您可能缺乏保存MOC链。为清楚起见,我已将关键字private替换为变量名backgroundMOC

关于上述问题,我只能假设行NSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; 在技术上类似于:

- (NSManagedObjectContext *)backgroundMOC:(NSManagedObjectContext *)mainMOC
{
    NSManagedObjectContext * threadManagedObjectContext = [[NSManagedObjectContext alloc]
                             initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [threadManagedObjectContext setParentContext:mainMOC];
    [threadManagedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
    return threadManagedObjectContext;
}

self.model.context作为mainMOC传递,使用self.model.contextsetMergePolicy
同样,我必须假设self.model saveWithErrorBlock在技术上与:

相同
[mainMOC performBlockAndWait:^{
    if([mainMOC hasChanges]) {
        NSError * error;
        [mainMOC save:&error];
        // handle error
    }
}];

如果是这样,backgroundMOC(您的private引用)也应如此:

[backgroundMOC performBlockAndWait:^{
    if([backgroundMOC hasChanges]) {
        NSError * error;
        [backgroundMOC save:&error];
        // handle error
    }
}];

换句话说,您希望确保backgroundMOCmainMOC保存操作是通过performBlockAndWait从各自的线程执行的。