Core data fails in 'validateForInsert'
For a project I'm working on I'm using core data to store the application's data. The application loads XML from the internet and tries to store the objects generated from the parsed results in the data model. This used to work fine, until a few 开发者_运维百科days ago. I changed the data model (added one property on an object), so I made a new version and ran mogenerator to generate new stub classes for the objects in the model. Most of it still works fine, but there are some odd errors in code that used to work perfectly.
During the parsing of the XML an object is created and values on it are filled in. One of the values is an URL for an image. In the data model this value may not be NIL, but in the XML it occasionaly is. I use validateForInsert on the item to check if I can commit it. This is the part that used to work fine, but now fails, complaining about the NIL value.
A bit of code from the parser:
...
currentItem = [[[MyItem alloc] initWithEntity:[self.dataModel entityByName:@"MyItem"] insertIntoManagedObjectContext:self.dataModel.managedObjectContext] autorelease];
currentItem.label = [attributeDict objectForKey:LABEL];
currentItem.paramString = [attributeDict objectForKey:QUERY_STRING];
[currentItem setSortOrderValue:[[currentRootItem items] count]];
[currentRootItem addItemsObject:currentItem];
} else if ([elementName isEqualToString:IMAGE]) {
currentItem.imageLocation = [attributeDict objectForKey:IMAGE_URL];
...
Then, when the document is parsed, I do the check:
...
{
NSArray *Items = [[myRootItem items] allObjects];
for (MyItem *item in Items) {
NSLog(@"%@", item);
NSLog(@"before");
NSLog(@"%d", [item validateForInsert:&validationError]);
NSLog(@"after");
if (![item validateForInsert:&validationError]) {
[[self.dataModel managedObjectContext] deleteObject:item];
}
}
}
...
This used to work just fine, but now it crashes in validateForInsert:
2011-03-30 13:38:32.951 xx[915:207] before
2011-03-30 13:38:33.130 xx[915:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: Property/imageLocation/Entity/Item)'
Stacktrace:
0 CoreFoundation 0x022fc5a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x02450313 objc_exception_throw + 44
2 CoreFoundation 0x022b4ef8 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x022b4e6a +[NSException raise:format:] + 58
4 CoreFoundation 0x022fae15 -[__NSCFDictionary setObject:forKey:] + 293
5 CoreData 0x013fa87c -[NSValidationErrorLocalizationPolicy _cachedObjectForKey:value:] + 172
6 CoreData 0x013fa629 -[NSValidationErrorLocalizationPolicy _localizedPropertyNameForProperty:entity:] + 201
7 CoreData 0x013fa4e7 -[NSValidationErrorLocalizationPolicy localizedPropertyNameForProperty:] + 71
8 CoreData 0x013a844e -[NSManagedObject(_NSInternalMethods) _substituteEntityAndProperty:inString:] + 142
9 CoreData 0x013a572e -[NSManagedObject(_NSInternalMethods) _generateErrorWithCode:andMessage:forKey:andValue:additionalDetail:] + 254
10 CoreData 0x013598f1 -[NSPropertyDescription(_NSInternalMethods) _nonPredicateValidateValue:forKey:inObject:error:] + 161
11 CoreData 0x01359485 -[NSAttributeDescription(_NSInternalMethods) _nonPredicateValidateValue:forKey:inObject:error:] + 85
12 CoreData 0x01358b22 -[NSManagedObject(_NSInternalMethods) _validateValue:forProperty:andKey:withIndex:error:] + 386
13 CoreData 0x01358847 -[NSManagedObject(_NSInternalMethods) _validatePropertiesWithError:] + 263
14 CoreData 0x013586e1 -[NSManagedObject(_NSInternalMethods) _validateForSave:] + 81
15 xx 0x001af8bf -[MyParser parserDidEndDocument:] + 1039
16 Foundation 0x00742717 _endDocument + 95
I can't figure out what went wrong. As far as I know making a new version of the data model went right (using XCode), setting it to the current version etc, all went right. I didn't have these problems the last time I did all this...
The only thing different is that now I upgraded to XCode 3.2.6, from 3.2.5 or 3.2.4, I don't remember.
My target is iPhone, and I'm using the 4.3 iOS SDK that came with this version of XCode.
I had this problem and I fixed it by checking my data length. If your model has a string field, you must validate that the String you're passing doesn't exceed the field length.
Well, to answer my own question...
The problem is not there in 3.2.5, nor is it there in 4.0.1. Looks like apple has some bugfixing to do on 3.2.6...
精彩评论