Filtering a CoreData FetchRequest

2019-09-19 07:25发布

问题:

I am having a brain meltdown trying to filter a fetchRequest.

I'm able to get the full details of every record of the entity, but I only want the details on the record at the current indexPath.

This is where my code is at present:

-(void) fetchStuff {

    NSLog(@"%s", __FUNCTION__); 
    NSError *error = nil;

    NSManagedObjectContext *context = [self managedObjectContext];      
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *woodgie = [NSEntityDescription entityForName:@"WidgetEntity" inManagedObjectContext:context];

    [fetchRequest setEntity:woodgie];

    NSArray *fetchResults = [managedObjectContext executeFetchRequest:fetchRequest error:&error];


    fetchedObjects = [fetchResults objectAtIndex:selectedRow];


    NSLog(@"selectedRow: %i", selectedRow);  //0

    NSLog(@"fetchedObjects: %@", fetchedObjects);



     for (WidgetEntity *wigglies in fetchedObjects  ) {

         NSSet *peopleSet = [wigglies valueForKey:@"people"];    
         for (id person in peopleSet) {          
             personName = [person valueForKey:@"name"];
             NSLog(@"name = %@", personName);
         }
         NSSet *keywordSet = [wigglies valueForKey:@"keyword"];  
         for (id keyWord in keywordSet) {
              keywordName = [keyWord valueForKey:@"word"];
             NSLog(@"Keyword = %@", keywordName);

.....
}

I get an exception at this line " for (WidgetEntity *wigglies in fetchedObjects ) {" (countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance)..

but the truth is - I'm guessing how to filter the data.

Any help / pointers will be appreciated.

回答1:

You are talking about fetching the record in the "row that has been selected" - this suggests that you have already fetched the records and are displaying them, presumably in a table view.

In that case there is no need to re-fetch the entities, simply use the object from the appropriate index in your data source array or your fetched results controller.



回答2:

-(void) fetchStuff {

    NSLog(@"%s", __FUNCTION__); 
    NSError *error = nil;

    NSManagedObjectContext *context = [self managedObjectContext];      
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *woodgie = [NSEntityDescription entityForName:@"WidgetEntity" inManagedObjectContext:context];

    [fetchRequest setEntity:woodgie];

    NSArray *fetchResults = [managedObjectContext executeFetchRequest:fetchRequest error:&error];


    fetchedRecord = [fetchResults objectAtIndex:selectedRow];


    NSLog(@"selectedRow: %i", selectedRow);  //0

    NSLog(@"fetchedRecord: %@", fetchedRecord); // this looks ok

Check the following two lines carefully.

     //for (WidgetEntity *wigglies in fetchResults  ) { // crash out here
     WidgetEntity *wigglies = fetchedRecord;

         NSSet *peopleSet = [wigglies valueForKey:@"people"];    
         for (id person in peopleSet) {          
             personName = [person valueForKey:@"name"];
             NSLog(@"name = %@", personName);
         }
         NSSet *keywordSet = [wigglies valueForKey:@"keyword"];  
         for (id keyWord in keywordSet) {
              keywordName = [keyWord valueForKey:@"word"];
             NSLog(@"Keyword = %@", keywordName);

.....
}