0
我對保存對象時收到的一個錯誤感到非常困惑。我收到以下錯誤(當我打印出詳細說明時):保存核心數據對象時出錯
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;
}
}];
}