views:

29

answers:

1

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];
}
A: 

I think the most likely problem is that you are passing the objectID for a NewsImages object instead of a for NewsItems object in setImage:ForObject: and in this line:

NewsItems *newsItem = (NewsItems*)[self.managedObjectContext objectWithID:moID];

...you are actually getting a NewsImages object returned cast as a NewsItems.

Cast can be problematical in Objective-C because an object will not complain at return if you send it a message it does not understand. The compiler won't catch the error because of the cast.

I would remove the cast, set the return item to id like so:

id newsItem = (NewsItems*)[self.managedObjectContext objectWithID:moID];

... then log its class:

NSLog(@"class=%@",[newItem class]);

... or check it in the debugger. Since you seem to be using custom NSManagedObject subclasses this should confirm the source of the error.

TechZen