Ios 使用多个语句预测

Ios 使用多个语句预测,ios,cocoa-touch,core-data,Ios,Cocoa Touch,Core Data,我有带有四个语句/参数的NSPredicate。似乎它们都没有“包括在内”。看起来是这样的: predicate = [NSPredicate predicateWithFormat:@"user.youFollow = 1 || user.userId = %@ && user.youMuted = 0 && postId >= %d", [AppController sharedAppController].currentUser.userId, sel

我有带有四个语句/参数的
NSPredicate
。似乎它们都没有“包括在内”。看起来是这样的:

predicate = [NSPredicate predicateWithFormat:@"user.youFollow = 1 || user.userId = %@ && user.youMuted = 0 && postId >= %d", [AppController sharedAppController].currentUser.userId, self.currentMinId.integerValue];
似乎忽略了最后一部分:
&&postId>=%d
。如果我尝试:

predicate = [NSPredicate predicateWithFormat:@"user.youFollow = 1 || user.userId = %@ && user.youMuted = 0 && postId = 0", [AppController sharedAppController].currentUser.userId, self.currentMinId.integerValue];

我得到了相同的结果(应该是0)。我想知道这样的谓词应该是什么样子的?

您能试试下面的代码吗

NSPredicate *youFollowPred = [NSPredicate predicateWithFormat:@"user.youFollow == 1"];
NSPredicate *userIdPred = [NSPredicate predicateWithFormat:@"user.userId == %@",[AppController sharedAppController].currentUser.userId];
NSPredicate *youMutedPred = [NSPredicate predicateWithFormat:@"user.youMuted == 0"];
NSPredicate *postIdPred = [NSPredicate predicateWithFormat:@"postId >= %d", self.currentMinId.integerValue];

NSPredicate *orPred = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:youFollowPred,userIdPred, nil]];

NSPredicate *andPred = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:youMutedPred,postIdPred, nil]];

NSPredicate *finalPred = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:orPred,andPred, nil]];

你能试试下面的代码吗

NSPredicate *youFollowPred = [NSPredicate predicateWithFormat:@"user.youFollow == 1"];
NSPredicate *userIdPred = [NSPredicate predicateWithFormat:@"user.userId == %@",[AppController sharedAppController].currentUser.userId];
NSPredicate *youMutedPred = [NSPredicate predicateWithFormat:@"user.youMuted == 0"];
NSPredicate *postIdPred = [NSPredicate predicateWithFormat:@"postId >= %d", self.currentMinId.integerValue];

NSPredicate *orPred = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:youFollowPred,userIdPred, nil]];

NSPredicate *andPred = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:youMutedPred,postIdPred, nil]];

NSPredicate *finalPred = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:orPred,andPred, nil]];

正如在讨论中所发现的,真正的问题是谓词在 获取的结果控制器,并且谓词中使用的变量随时间变化

在这种情况下,必须重新创建谓词和获取请求。这是有案可查的 在中的“修改提取请求”

因此,在您的情况下,如果
self.currentMinId
发生更改,您应该

// create a new predicate with the updated variables:
NSPredicate *predicate = [NSPredicate predicateWithFormat:...]
// create a new fetch request:
NSFetchRequest *fetchRequest = ...
[fetchRequest setPredicate:predicate];

// Delete the section cache if you use one (better don't use one!)
[self.fetchedResultsController deleteCacheWithName:...];

// Assign the new fetch request and re-fetch the data:
self.fetchedResultsController.fetchRequest = fetchRequest;
[self.fetchedResultsController performFetch:&error];

// Reload the table view:
[self.tableView reloadData];

正如在讨论中所发现的,真正的问题是谓词在 获取的结果控制器,并且谓词中使用的变量随时间变化

在这种情况下,必须重新创建谓词和获取请求。这是有案可查的 在中的“修改提取请求”

因此,在您的情况下,如果
self.currentMinId
发生更改,您应该

// create a new predicate with the updated variables:
NSPredicate *predicate = [NSPredicate predicateWithFormat:...]
// create a new fetch request:
NSFetchRequest *fetchRequest = ...
[fetchRequest setPredicate:predicate];

// Delete the section cache if you use one (better don't use one!)
[self.fetchedResultsController deleteCacheWithName:...];

// Assign the new fetch request and re-fetch the data:
self.fetchedResultsController.fetchRequest = fetchRequest;
[self.fetchedResultsController performFetch:&error];

// Reload the table view:
[self.tableView reloadData];

你试过用括号吗?只是想看看这是否是一个优先级问题……我试过:
(user.youFollow=1 | | user.userId=%@)和&(user.youMuted=0&&postId>=%d)
。NSLog(@“%@,[predicate description])是什么意思打印?您可以尝试删除谓词的某些“部分”(简化它),以查看结果是否符合预期……当一个逻辑条件中存在OR和运算时,必须使用括号。此外,使用
=
代替
=
进行比较。尝试使用
而不是
|
&
。如果要比较两个字符串,请使用
'
符号:
user.userId=='%@'
。是否尝试使用括号?只是想看看这是否是一个优先级问题……我试过:
(user.youFollow=1 | | user.userId=%@)和&(user.youMuted=0&&postId>=%d)
。NSLog(@“%@,[predicate description])是什么意思打印?您可以尝试删除谓词的某些“部分”(简化它),以查看结果是否符合预期……当一个逻辑条件中存在OR和运算时,必须使用括号。此外,使用
=
代替
=
进行比较。尝试使用
而不是
|
&
。如果要比较两个字符串,请使用
'
符号:
user.userId='%@'
。谢谢,它可以工作。您是否知道-
NSPredicate*postedpredicate=[NSPredicate predicateWithFormat:@“postId>=%d”,self.currentMinId.integerValue];-可以有一个动态的
%d`变量。在我进行新的抓取之前,需要更新谓词。@sunilz:请注意,您的代码与谓词
(user.youFollow=1 | | | user.userId=%@)和&(user.youmute=0&&postId>=%d)”,…
完全相同。谢谢,它可以工作。您是否知道-
NSPredicate*postedpredicate=[NSPredicate predicateWithFormat:@“postId>=%d”,self.currentMinId.integerValue];-可以有一个动态的
%d`变量。在我进行新的抓取之前,需要更新我的谓词。@sunilz:请注意,您的代码与谓词
(user.youFollow=1 | | | user.userId=%@)和&(user.youmute=0&&postId>=%d)”,…
完全相同。谢谢,它成功了!请注意,setFetchRequest是只读的。我做到了:
self.fetchedResultsController.fetchRequestSetPredicate…
@Anders:谢谢你的反馈
self.fetchedResultsController.fetchRequest setPredicate:newPredicate]
也可以,也许您想试试。在这种情况下,您只需要一个新的谓词,而不需要一个新的获取请求。谢谢,让它工作了!请注意,setFetchRequest是只读的。我做到了:
self.fetchedResultsController.fetchRequestSetPredicate…
@Anders:谢谢你的反馈
self.fetchedResultsController.fetchRequest setPredicate:newPredicate]
也可以,也许您想试试。在这种情况下,您只需要一个新的谓词,而不需要一个新的获取请求。