FetchRequest 上的 If 语句
我该如何做到这一点,以便我只获取指标 == 1 的结果。(我将指标连接到另一个视图上的 UISwitch)。所以我想要一个标题表,但前提是指标为 1。
-(void)reload
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Title" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc]initWithKey:@"indicator" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
[self setList:mutableFetchResults];
[mutableFetchResults release];
[request release];
}
How do I make it so I only fetch results with the indicator == 1. (I have the indicator hooked up to a UISwitch on another view). so I want a table of titles, but only if the indicator is 1.
-(void)reload
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Title" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc]initWithKey:@"indicator" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
[self setList:mutableFetchResults];
[mutableFetchResults release];
[request release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
向您的提取请求添加谓词:
这将导致仅返回设置了该谓词的事件。我还会从排序描述符中删除您的 sortDescriptor2 。当指标键都具有相同的值时,对它们进行排序是没有意义的。
Add a predicate to your fetch request:
That'll cause only the events that have it set to be returned. I'd also remove your sortDescriptor2 from the sort descriptors. No sense doing to the work to sort of the indicator key when they'll all have the same value.