Exception Caused by Predicate with CONTAINS
I am getting this weird exception and I have no clue how to figure out what's going wrong here
"Serious application error. **Exception was caught during Core Data change processing.** This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. **Can't look for value (552284302) in string (1070635162, 630076961, 690758344, 714304224, 693603903, 650263092, 552284302); value is not a string with userInfo (null)**"
My code is as follows:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSManagedObjectContext *context = [ (MyAppDelegate*)[[UIApplication sharedApplication] delegate]managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"MyEntityName" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"distance" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
User *user = [[DatabaseHelper sharedInstance] getUserDetails];
NSString 开发者_JS百科*predicteString = [NSString stringWithFormat:@"isPrivate == %@ AND recepientList contains %@", [NSNumber numberWithBool:YES], user.userId];
if(dootType == UnReadDootType){
predicteString = [predicteString stringByAppendingString:@" AND state = 'NEW' "];
}
NSPredicate *predicate;
if(senderId != nil && [senderId intValue] > 0){
predicteString = [predicteString stringByAppendingString:@" AND senderId == %@"];
predicate = [NSPredicate
predicateWithFormat:predicteString, senderId];
}else{
predicate = [NSPredicate
predicateWithFormat:predicteString];
}
[fetchRequest setPredicate:predicate];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context sectionNameKeyPath:nil
cacheName:@"MyEntityName"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[sort release];
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
The NSFetchRequest logs as:
<NSFetchRequest: 0x362350> (entity: MyEntityName; predicate: (isPrivate == 1 AND recepientList CONTAINS 552284302); sortDescriptors: ((
"(distance, ascending, compare:)"
)); batch size: 20; type: NSManagedObjectResultType; )
So as you can see I am using a predicate like recepientList CONTAINS 552284302
and for one of my record the recepientList has 1070635162, 630076961, 690758344, 714304224, 693603903, 650263092, 552284302
I am storing comma separated userId in the recepientList, so I am not sure why this exception.
The user.userID
is being parsed as a numerical value instead of a string. The CONTAINS
operator does not understand numerical values.
The predicate that logs as:
(isPrivate == 1 AND recepientList CONTAINS 552284302)
… should log as:
(isPrivate == 1 AND recepientList CONTAINS "552284302")
The former predicate looks for a numerical value while later looks for a string. (Note that the numerical one in isPrivate == 1
is not quoted.)
If user.userID
is a NSNumber then getting it's string value should correct the problem. Try:
NSString *predicteString = [NSString stringWithFormat:@"isPrivate == %@ AND recepientList contains %@", [NSNumber numberWithBool:YES], [user.userId stringValue]];
Thanks to @TechZen, his answer gave me a hint to figure out the right answer.
@TechZen you were absolutely correct that the predicate should log as
(isPrivate == 1 AND recepientList CONTAINS "552284302")
But the thing is user.userId is not a NSNumber it is a NSString, so the problem is with this line
NSString *predicteString = [NSString stringWithFormat:@"isPrivate == %@ AND recepientList contains %@", [NSNumber numberWithBool:YES], user.userId];
I changed this to
NSString *predicteString = [NSString stringWithFormat:@"isPrivate == %@ ", [NSNumber numberWithBool:YES]];
predicteString = [predicteString stringByAppendingString:@"AND recepientList contains %@"];
predicate = [NSPredicate
predicateWithFormat:predicteString, user.fbUserId];
and that's it now I get it as expected. I don't know why NSString stringWithFormat is treating a NSString as a NSNumber, while NSPredicate treat it in a right way.
精彩评论