CONTAINS谓词引起的异常

时间:2011-09-18 15:31:38

标签: ios exception core-data

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

"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)**"

我的代码如下:

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

}

NSFetchRequest记录为:

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

因为你可以看到我正在使用像recepientList CONTAINS 552284302这样的谓词 对于我的一条记录,recepientList有1070635162, 630076961, 690758344, 714304224, 693603903, 650263092, 552284302我将逗号分隔的userId存储在recepientList中,所以我不确定为什么会出现这种异常。

2 个答案:

答案 0 :(得分:6)

user.userID被解析为数值而不是字符串。 CONTAINS运算符不理解数值。

记录为:

的谓词
(isPrivate == 1 AND recepientList CONTAINS 552284302)

...应记录为:

(isPrivate == 1 AND recepientList CONTAINS "552284302")

前一个谓词查找数值,后来查找字符串。 (请注意,未引用isPrivate == 1中的数字。)

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

NSString *predicteString = [NSString stringWithFormat:@"isPrivate == %@  AND recepientList contains %@", [NSNumber numberWithBool:YES], [user.userId stringValue]];

答案 1 :(得分:1)

感谢@TechZen,他的回答给了我一个提示,找出正确的答案。

@TechZen你绝对正确,谓词应记录为

(isPrivate == 1 AND recepientList CONTAINS "552284302")

但问题是user.userId不是NSNumber它是NSString,所以问题在于这行

NSString *predicteString = [NSString stringWithFormat:@"isPrivate == %@  AND recepientList contains %@", [NSNumber numberWithBool:YES], user.userId];

我将此更改为

 NSString *predicteString = [NSString stringWithFormat:@"isPrivate == %@ ", [NSNumber numberWithBool:YES]];

 predicteString = [predicteString stringByAppendingString:@"AND recepientList contains %@"];
 predicate = [NSPredicate
                 predicateWithFormat:predicteString, user.fbUserId];

现在我就按预期得到了它。我不知道为什么NSString stringWithFormat将NSString视为NSNumber,而NSPredicate以正确的方式处理它。