核心数据一对多关系设置为零

时间:2016-02-24 11:50:40

标签: core-data

我有2张桌子A和B. A - >> B(AToBRelation,一对多关系) A< -B(BToARelation,一对一关系)

我尝试在A中保存一行,这很好。 然后我尝试在B中保存一行,我将获取A的实体,然后执行addAToBRelationRelationshipObject(CoreDataGeneratedAccessors)。这也很好。但是当我尝试在for循环中非常快地执行此操作时30-40 B的实体(每个都按照我上面的说法一个接一个地保存),我得到一个核心数据错误mainy 1550错误代码。错误描述显示关系为零。

控制台日志:         未解决的错误错误域= NSCocoaErrorDomain代码= 1550"操作无法完成。 (可可错误1550。)" UserInfo = {悬挂对无效对象的引用。= null,NSValidationErrorValue = Relationship' messageRelationship'在托管对象上

    - (RJobObject *)saveMessageToDB:(RMessageObject *)msgObject {

    NSManagedObjectContext *context = [self managedObjectContext];

    RJobEntity * jobEntity = [self getJobEntityWithId:msgObject.jobId];
    if (jobEntity == nil) {
        //this portion cannot be reached
        return nil;
    }


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"RNewMessageEntity" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"id == %@", msgObject.id]];
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];

    if (fetchedObjects.count == 0) {

        RKObjectMapping * msgMapping = [RMessageObject mapping];
        RKRequestDescriptor *requestDescriptor2 = [RKRequestDescriptor requestDescriptorWithMapping:msgMapping.inverseMapping objectClass:[RMessageObject class] rootKeyPath:nil method:RKRequestMethodPOST];
        NSDictionary *dict = [RKObjectParameterization parametersWithObject:msgObject requestDescriptor:requestDescriptor2 error:nil];

        RNewMessageEntity * msgEntity  = [NSEntityDescription insertNewObjectForEntityForName:@"RNewMessageEntity" inManagedObjectContext:context];
        msgEntity.dictionary = dict;
        msgEntity.id = msgObject.id;
        msgEntity.createdAt = [NSNumber numberWithLongLong:msgObject.createdAt];

        jobEntity.latestMessageCreatedAt = [NSNumber numberWithLongLong:msgObject.createdAt];
        [jobEntity addMessageRelationshipObject:msgEntity];
        if (jobEntity.messageRelationship == nil) {
             NSLog(@"swappy");
        }

        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Failed to save in core data : %@", error.localizedDescription);
            return nil;
        }

        RJobObject *jobObject = [self getJobObjectFromEntity:jobEntity];
        return jobObject;
    } 

    return nil;
}

- (RJobObject *)saveJobToDB:(RJobObject *)jobObject {

    NSManagedObjectContext *context = [self managedObjectContext];

    RJobEntity * jobEntity = [self getJobEntityWithId:jobObject.id];

    if (!jobEntity) {
        jobEntity = [NSEntityDescription insertNewObjectForEntityForName:@"RJobEntity" inManagedObjectContext:context];
    }

    RKObjectMapping * itemMapping = [RJobObject mapping];
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:itemMapping.inverseMapping objectClass:[RJobObject class] rootKeyPath:nil method:RKRequestMethodPOST];

    NSMutableDictionary *dict = (NSMutableDictionary *)[RKObjectParameterization parametersWithObject:jobObject requestDescriptor:requestDescriptor error:nil];

    jobEntity.pUserJobsDictionary = dict;
    jobEntity.id = jobObject.id;

    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Failed to save in core data : %@", error.localizedDescription);
        return nil;
    }

    jobObject.latestMessageCreatedAt = jobEntity.latestMessageCreatedAt;
    return jobObject;

}

Core Data model

0 个答案:

没有答案