iOS App Crashing When Adding a New Entry after A Search with TableViewControllers using Core Data and NSFetchedResultsControllers

StackOverflow https://stackoverflow.com/questions/21880434

Question

I am facing an interesting issue with my app. Firstly the premise of the app is simple: A table view controller with a plus button in the navigation bar; a user presses the plus and a view controller appears modally. The user adds in a name, amount and event, presses save and through NSFetchedResultsController and Core Data, that information is successfully populated back in the Table View Controller.

That works very well. The TableViewController is also using a SearchBar so that the user can search for names or events in the Table View.

The issue is:

If a user searches for an entry in the Table View (and does not cancel out of the search) and then presses the Add button to add an entry, fills in the information and presses save, the app crashes with the error below:

CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet. with userInfo (null)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet.'

However, if the user does actually cancel the search from the Table View and then adds a new entry, it does not crash. This is something I need to fix because some workflows may not see the need to cancel the search before adding a new entry.

My NSFetchedResultsController in the Table View has the following predicate:

if ([self.timelineSearchBar.text length] > 0) {
    NSPredicate *predName = [NSPredicate predicateWithFormat:@"ANY whoBy.name CONTAINS[c] %@", self.timelineSearchBar.text];
    NSPredicate *predOccasion = [NSPredicate predicateWithFormat:@"ANY occasion.title CONTAINS[c] %@", self.timelineSearchBar.text];

    // creating the orCompoundPredicate for the two conditions
    NSPredicate *compPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predName, predOccasion, predDate]];
    [fetchRequest setPredicate:compPredicate];
}

I understand what is happening, but I'm not entirely sure how to go about fixing it. The error appears to be very common and there are lots of fixes out there, all relating to slightly adjusting the predicates, but I'm not sure which direction to take with this.

I am adding a view of my model for further information as well.

enter image description here Any assistance on this would be massively appreciated.

EDIT: I am presenting the view controller through storyboard rather than in code, but as per the comment by TheEye, it might be easier to actually just clear the search in code and present the view controller manually. I'm looking into exploring this as a way forward.

Was it helpful?

Solution

That error occurs if "ANY" is used with a to-one relationship instead of a to-many relationship. For example, if "occasion" is a to-one relationship then you have to replace

@"ANY occasion.title CONTAINS[c] %@"

by

@"occasion.title CONTAINS[c] %@"

in the second predicate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top