Search code examples
xcodeios6nsfetchrequest

Xcode - filter an NSFetchRequest and select each object


I am trying to filter a fetchRequest.

I'm at the point where the result is loaded into an NSArray.

However, I need to parse the array to pull out the individual items - right now, they look as if they were one object.

The code I'm using to get to this point is:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSManagedObjectContext *moc = coreDataController.mainThreadContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:moc];

[request setEntity:entity];


    // Order the events by name.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

    [request setSortDescriptors:@[sortDescriptor]];

    // Execute the fetch -- create a mutable copy of the result.
    NSError *error = nil;
    NSArray *categories = [[moc executeFetchRequest:request error:&error] mutableCopy];

    if (categories == nil) {
        NSLog(@"bugger");
    }

    NSObject *value = nil;
    value = [categories valueForKeyPath:@"name"];

This results as follows:

 value = (
)

[DetailViewController loadPickerArray]
[AppDelegate loadPickerArray]
 value = (
    "Cat Two",
    "Cat Three",
    "Cat One",
    "Cat Four"
)

Also, please note that the first time this ran, there were no results. I get that about 50% of the time.

Thanks for any help.


Solution

  • There are several methods you can filter your data.

    The preferred way is to use a predicate for your search. This will give you the best performance.

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSManagedObjectContext *moc = coreDataController.mainThreadContext;
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:moc];
    
    [request setEntity:entity];
    
    // Order the events by name.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[CD] %@", @"Cat"]; //This will return all objects that contain 'cat' in their name property.
    
    [request setPredicate:predicate];
    [request setSortDescriptors:@[sortDescriptor]];
    
    // Execute the fetch -- create a mutable copy of the result.
    NSError *error = nil;
    NSArray *categories = [moc executeFetchRequest:request error:&error];
    
    if (categories == nil) {
        NSLog(@"bugger");
    }
    
    //Here you have the objects you want in categories.
    
    for(Category *category in categories)
    {
        NSLog(@"Category name: %@", category.name);
    }
    

    If you wish to filter using an array, the following is possible also:

    NSMutableArray *categories = [[moc executeFetchRequest:request error:&error] mutableCopy];
    
    [categories filterUsingPredicate:[NSPredicate predicateWithFormat:[NSPredicate predicateWithFormat:@"name CONTAINS[CD] %@", @"Cat"]]
    
    //Now, the only objects left in categories will be the ones with "cat" in their name property.
    

    I recommend reading the Predicates Programming Guide, as predicates are very powerful, and it is much more efficient to filter your results in the store.