I have a Uitableview that loads data from a parsed xml feed. when the feed is first parsed all the text data is stored in an entity NewsItems in core data. after the table is loaded the images asociated with each object are fetched asynchronously and stored in a separate entity NewsImages, after the feeds/images are stored locally all data is fetched locally next time the app starts. NewsItems and NewsImages have a one to one relationship with each other.
I have a refresh button which when clicked deletes all entries in NewsItems, this will also delete all objects in NewsImages associated with objects in NewsItems aswell, since the relationship delete rules are cascade. After deleteion, the feed is parsed again and data is stored locally again.
My problem is when I do this multiple number of times quickly. I get this error while saving Images locally.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-one relationship: property = "ItemImage"; desired type = NewsImages; given type = NewsImages; value = <NewsImages: 0x68c49f0> (entity: NewsImages; id: 0x6804730 <x-coredata:///NewsImages/t5444BEE7-6193-4C25-8AAB-F64113BEAB7546> ; data: {
Image = <ffd8ffe0 00104a46 49460001 01000001 00010000 ffe10058 45786966 00004d4d 002a0000 00080002 01120003 00000001 0001>;
ImageItem = nil;
}).'
This is the function responsible for inserting images
-(void)setImage:(UIImage*)moImage ForObject:(NSManagedObjectID*)moID{
NewsItems *newsItem = (NewsItems*)[self.managedObjectContext objectWithID:moID];
NewsImages *newsImage = (NewsImages*)[NSEntityDescription insertNewObjectForEntityForName:@"NewsImages" inManagedObjectContext:self.managedObjectContext];
newsImage.Image = UIImageJPEGRepresentation(moImage,1.0);
newsItem.ItemImage = newsImage;
[self commitSave];
}