Search code examples
iphoneobjective-cioscore-datacore-data-migration

CoreData could not fulfill a fault for retrieving data from an xml feed and using managed objects to manipulate


I have the Error - CoreDate could not fulfill a fault for retrieving data from.. The application crashes right after Item *currentItem = (Item *)[self.fetchedObjectsArray objectAtIndex:currentItemIndex] in the below code. It did however show a couple of feeds and then crashed. Also reproducing the error is tough and even if it does get reproduced it crashes after a different number of feeds have been received. Would anyone know how i would go about debugging a problem like this?

- (void) showNextFeed {
if ([self.fetchedObjectsArray count] <= 0) 
     return;
if (feedAnimating) 
     return;

feedAnimating = YES;

//start off the left of the screen
self.newsTitleView.center = CGPointMake(-self.newsTitleView.frame.size.width * 0.5, self.newsTitleView.center.y);

//start off the right of the screen
self.newsItemTitleLabel.center = CGPointMake(self.view.frame.size.width + self.newsItemTitleLabel.frame.size.width * 0.5, self.newsItemTitleLabel.center.y);

//update the content
currentItemIndex = (currentItemIndex + 1 < [self.fetchedObjectsArray count]) ? (currentItemIndex + 1) : 0;
Item *currentItem = (Item*)[self.fetchedObjectsArray objectAtIndex:currentItemIndex];
self.newsTitleView.backgroundColor = [UIColor colorWithHexString:currentItem.belongsTo.color];
self.newsFeedTitleLabel.text = currentItem.belongsTo.feedTitle;
self.newsItemTitleLabel.text = currentItem.itemTitle;

Solution

  • You should first of all make sure that currentItemIndex (which I suppose is a class ivar) is properly initialized. It is probably worth checking where else it gets manipulated.

    The same goes for fetchedObjectsArray. Where else is it being changed and could the change and your method interfere with each other?

    Indeed the error indicates that an item in core data is already deleted or changed when the view still needs it (as is the case in your up showNextFeed method).

    Finally, did you consider using a NSFetchedResultsController? The threading and error checking is much easier with this and you can leave most of the tricky persistent store updates and fetches to core data rather than maintaining your own results array. (Already, you have a lot of code just to check you are not out of bounds...)