iOS How to use NSFetchedResultsController for condition fetch

mikezang

macrumors 6502a
Original poster
May 22, 2010
772
1
Tokyo, Japan
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;
}
 

CodeBreaker

macrumors 6502
Nov 5, 2010
494
1
Sea of Tranquility
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;
}
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.
 

mikezang

macrumors 6502a
Original poster
May 22, 2010
772
1
Tokyo, Japan
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.
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?
 

CodeBreaker

macrumors 6502
Nov 5, 2010
494
1
Sea of Tranquility
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?
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.
 

mikezang

macrumors 6502a
Original poster
May 22, 2010
772
1
Tokyo, Japan
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.
Great! Your idea is wonderful!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.