CONTAINS谓词引起的异常

Yog*_*esh 1 exception core-data ios

我得到了这个奇怪的例外,我不知道如何弄清楚这里出了什么问题

"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)**"
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

- (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 *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;    

}
Run Code Online (Sandbox Code Playgroud)

NSFetchRequest记录为:

<NSFetchRequest: 0x362350> (entity: MyEntityName; predicate: (isPrivate == 1 AND recepientList CONTAINS 552284302); sortDescriptors: ((
    "(distance, ascending, compare:)"
)); batch size: 20; type: NSManagedObjectResultType; )
Run Code Online (Sandbox Code Playgroud)

因此,你可以看到我正在使用谓词recepientList CONTAINS 552284302 和我的记录之一,recepientList我在recepientList中1070635162, 630076961, 690758344, 714304224, 693603903, 650263092, 552284302存储逗号分隔的userId,所以我不知道为什么会出现这种异常.

Tec*_*Zen 6

所述user.userID被解析为数值而不是字符串.该CONTAINS运营商不理解数值.

记录为的谓词:

(isPrivate == 1 AND recepientList CONTAINS 552284302)
Run Code Online (Sandbox Code Playgroud)

...应记录为:

(isPrivate == 1 AND recepientList CONTAINS "552284302")
Run Code Online (Sandbox Code Playgroud)

前一个谓词查找数值,然后查找字符串.(注意,isPrivate == 1未引用数字1 ).

如果user.userID是NSNumber,那么获取它的字符串值应该可以解决问题.尝试:

NSString *predicteString = [NSString stringWithFormat:@"isPrivate == %@  AND recepientList contains %@", [NSNumber numberWithBool:YES], [user.userId stringValue]];
Run Code Online (Sandbox Code Playgroud)