开发者

If statement on FetchRequest

开发者 https://www.devze.com 2023-03-01 16:14 出处:网络
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.

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;开发者_Go百科 
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    [self setList:mutableFetchResults]; 
    [mutableFetchResults release]; 
    [request release];
}


Add a predicate to your fetch request:

[request setPredicate:[NSPredicate predicateWithFormat:@"indicator == 1"]];

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消