保存核心数据对象时出错

时间:2015-05-08 15:26:53

标签: objective-c core-data

我对保存对象时收到的一个错误感到很困惑。我收到以下错误(当我打印出详细说明时):

2015-05-08 08:19:51.589 Br[11240:208443] Core Data Save Error

                           NSValidationErrorKey
activityObject

                           NSValidationErrorPredicate
(null)

                           NSValidationErrorObject
<BRActivity: 0x7deb2aa0> (entity: BRActivity; id: 0x7deb2780 <x-coredata:///BRActivity/t86C0E8CD-2B6C-4AF7-986A-4797B7BEFDF5> ; data: {
activities =     (
);
activityObject = nil;
activityType = 0;
body = "Someone left a comment on your post.";
timestamp = "2015-05-08 04:24:46 +0000";
uuidString = "c6a06b45-e2d5-45bd-9f64-20b13ac87526";
})

                           NSLocalizedDescription
The operation couldn’t be completed. (Cocoa error 1570.)
2015-05-08 08:19:51.590 Br[11240:208443] Core Data Save Error

因此,从互联网上看,这似乎是一种关系问题。所以一个Br帖子有一个叫做活动关系的关系,其中一个关系是一个活动对象。现在,正如我们从错误中看到的那样,是零。那么我们在这里看到什么样的解决方案...有没有办法让关系“可选”(确定为零!)或者我应该添加一个活动对象?我真的不想在这里打破任何东西,所以如果有一个微妙的解决方案让我知道。谢谢一群人!

此处还有围绕保存的方法:

 - (void)importArray:(NSArray *)array entityName:(NSString *)entityName attributeName:attributeName error:(NSError *__autoreleasing *)error {
NSParameterAssert(array);
NSParameterAssert(entityName);

[self.context performBlockAndWait:^{
    for (NSDictionary *jsonDictionary in array) {
        NSManagedObject *managedObject = [NSManagedObject upsertWithContext:self.context entityName:entityName dictionary:jsonDictionary attributeName:attributeName error:error];
        if (nil == managedObject) {
            if ([self.context hasChanges]) {
                [self.context rollback];
            }
            return;
        }
    }

    if ([self.context hasChanges]) {
        if (![self.context save:error]) {
            NSError *err = *error;


            NSDictionary *userInfo = [err userInfo];
            if ([userInfo valueForKey:@"NSDetailedErrors"] != nil) {
                // ...and loop through the array, if so.
                NSArray *errors = [userInfo valueForKey:@"NSDetailedErrors"];
                for (NSError *anError in errors) {

                    NSDictionary *subUserInfo = [anError userInfo];
                    subUserInfo = [anError userInfo];
                    // Granted, this indents the NSValidation keys rather a lot
                    // ...but it's a small loss to keep the code more readable.
                    NSLog(@"Core Data Save Error\n\n \
                          NSValidationErrorKey\n%@\n\n \
                          NSValidationErrorPredicate\n%@\n\n \
                          NSValidationErrorObject\n%@\n\n \
                          NSLocalizedDescription\n%@",
                          [subUserInfo valueForKey:@"NSValidationErrorKey"],
                          [subUserInfo valueForKey:@"NSValidationErrorPredicate"],
                          [subUserInfo valueForKey:@"NSValidationErrorObject"],
                          [subUserInfo valueForKey:@"NSLocalizedDescription"]);
                }
            }


            NSLog(@"Error: %@", err.localizedDescription);
        }
        return;
    }
}];
}

1 个答案:

答案 0 :(得分:0)

是的,关系可以是可选的。选择关系,您将在右上角的“数据检查器”窗格中看到可选的可选项。