How to use NSFetchedResultsController for condition fetch

Discussion in 'iOS Programming' started by mikezang, Oct 18, 2012.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I was used NSArray to read data in Core Data and show them to table view cell. there is no problems.
    But when I tried to use NSFetchedResultsController, I got error as below if I scroll table view, what do I have to pay attention?

    Code:
    *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
    
    my code as below
    Code:
    - (NSFetchedResultsController *)fetchedResultsController {
        if (fetchedResultsController_ != nil) {
            return fetchedResultsController_;
        }
        
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:RHYME_ENTITY inManagedObjectContext:self.managedObjectContext];
        
        [fetchRequest setEntity:entity];
        
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"code" ascending:NO];
    
        [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
        
        [fetchRequest setFetchBatchSize:20];
        
        NSPredicate *filter = [NSPredicate predicateWithFormat:@"code like %@", @"???001"];
        [fetchRequest setPredicate:filter];
        
        NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc]
                                                                   initWithFetchRequest:fetchRequest
                                                                   managedObjectContext:self.managedObjectContext
                                                                   sectionNameKeyPath:nil cacheName:@"Root"];
        self.fetchedResultsController = theFetchedResultsController;
        self.fetchedResultsController.delegate = self;
        
        return self.fetchedResultsController;
    }
    
     
  2. CodeBreaker macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #2
    It does not crash in the code you posted. It must be crashing in your table view delegate/datasource methods. Paste them. Also to speed up debugging, add exception breakpoints. It will show where exactly your app crashed.

    Most likely you forgot to remove some of the code from your earlier implementation using an array.
     
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    Thanks for your message, I am not sure how to use this for multi sections table view, my data has a code like "10101", I want to show data in the same section when code like "101???", and another section when code like "102???"...
    Do you have any idea to use code as above for my purpose?
     
  4. CodeBreaker macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #4
    If I was in your place, I would had added another attribute to the entity called codePrefix. The values in it would had been the first three numbers of your code (like 101, 102, 103, and so on.).

    Then I would had set the codePrefix as the sectionNameKeyPath where I allocate the fetched results controller with this initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:.

    After that it should handle it automatically.
     
  5. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
    Great! Your idea is wonderful!
     

Share This Page