at_yasu's blog

ロード的なことを

保存の際の例外処理

「Operation could not be completed. (Cocoa error 1570.)」でずっとハマってたのでメモ。

正常に処理できませんでした(Cocoa error 1570.)としか言われずハマってたのですが、どうやらここを読むとCoreDataの保存の際に起きてるようで。

でも何で保存に失敗してるか解らないので、少しコードを下記の様にして、出力を煩くしてみる。

    NSError *error = nil;
    if (![[self managedObjectContext] save:&error]) {
		NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
		NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
		if(detailedErrors != nil && [detailedErrors count] > 0) {
			for(NSError* detailedError in detailedErrors) {
				NSLog(@"  DetailedError: %@", [detailedError userInfo]);
			}
		}
		else {
			NSLog(@"  %@", [error userInfo]);
		}
    }


出力

2009-10-05 05:08:33.445 ReinRous[772:20b] Failed to save to data store: Operation could not be completed. (Cocoa error 1560.)
2009-10-05 05:08:33.447 ReinRous[772:20b]   DetailedError: {
    NSLocalizedDescription = "Operation could not be completed. (Cocoa error 1570.)";
    NSValidationErrorKey = isNew;
    NSValidationErrorObject = <RREntry: 0x3fab6b0> (entity: RREntitiy; id: 0x3eef970 <x-coredata://.../p31> ; data: {
    User = 0x3f9d300 <x-coredata://.../p9>;
    date = 2009-10-05 04:54:30 +0900;
    inReplyUniqID = 0;
    isNew = nil;
    keyword = 0x3f9dc30 <x-coredata://.../p1>;
    message = "...";
    reply = nil;
    uniqID = 2147483647;
});
}
2009-10-05 05:08:33.447 ReinRous[772:20b]   Detail...


NSValidationError に値が入っているので、どうもDBのフィールドに入れた値が間違ってたようです。

実際、「obj.isNew = NO」とかにしていました。