开发者

Core Data NSInvalidArgumentException Error

I am getting a crash when changing a BOOL attribute of my NSManagedObject.

The code to save the object is:

self.detailItem.bookmark = [NSNumber numberWithBool:YES];
NSError *error = nil;
if (!    [self.detailItem.managedObjectContext save:&error]) 
{
    // Handle the error.
}
NSLog(@"%@", error);

And the error:

Serious application error.  Exception was caught d开发者_高级运维uring 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)
2011-08-18 15:41:32.866 Codes[5260:707] *** 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.'


WARNING: This answer is based on a guess I've made based on the error message; it might be 100% wrong!


The error looks like it's an incorrectly formed NSPredicate. You might have something like

NSString *name = @"Bob";
[NSPredicate predictaeWithFormat:@"ANY %K IN %@", @"name", name];

That looks like it might find anything where name = 'Bob' but it won't, it will throw an exception :(

If you're using 'IN' you need to pass an NSSet or NSArray i.e .

NSArray *names = [NSArray arrayWithObjects:@"Alice", @"Bob", nil];
[NSPredicate predictaeWithFormat:@"ANY %K IN %@", @"name", names];

This will find anything with the name 'Bob' or 'Alice'.

If you just wanted to search for 'Bob', just do this :

NSString *name = @"Bob";
[NSPredicate predictaeWithFormat:@"%K == %@", @"name", name];


I think deanWornbourne wrote the right answer, but it wasn't very clear to me so I'm going to restate his answer and insights, and provide a further explanation.

ProgramGuy was getting the serious application error because his predicate was wrong. The reason why ANY bookmark == YES is wrong is that "ANY" is only used where you have a one-to-many relationship in your model. When you have a one-to-one relationship, you should use bookmark == YES.

The 'serious application error' is a bit silly sounding and causes more alarm than necessary. However, it gives you a pretty good clue to the error. It says there is a 'bug within an observer..'. That means you have some 'NSFetchedResults' object or 'NSFetchedResultsController' setup (since we're talking about the NSManagedObjectContextObjectChangedNotification). Also, the error message says left hand side for an ALL or ANY operator must be either an NSArray... This is hinting that there must be a one-to-many relationship.

Hopefully that helps.


Just an FYI. deanWombourne's answer was 100% correct in my case.

NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyTeamSchedule" inManagedObjectContext:[[MyTeamStore sharedStore] context]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY myteam.teamname == %@", myTeamName];

After removing the ANY the problem was solved. Thank god for dean! Just saved me from many hours of troubleshooting and headaches.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜