gpt4 book ai didi

ios - 由CONTAINS谓词引起的异常

转载 作者:行者123 更新时间:2023-12-01 17:34:50 25 4
gpt4 key购买 nike

我收到了这个怪异的异常,我不知道如何弄清楚这里出了什么问题

"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中,所以我不确定为什么会出现此异常。

最佳答案

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

关于ios - 由CONTAINS谓词引起的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7462475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com