Question

I'm trying to fetch my data, but the results are empty!
First, I have a table, with data from one NSFetchedResultsController. Until here, my code is right, and also I know I have the data.
But then, I filter that NSFetchedResultsController, so I need another NSFetchedResultsController with the records without filter, I mean, all my records. So, I'm trying to fetch the data again, with another NSFetchedResultsController, using the same code, but the results is always empty!!!.
This is the functions that I'm using:

+(NSFetchedResultsController*)fetchedResultsControllerForEntity:(NSString*)entityName titleKey:(NSString*)titleKey sectionNameKeyPath:(NSString*)sectionNameKeyPath{

      NSFetchRequest *request=[self fetchRequestForEntity:entityName titleKey:titleKey sectionNameKeyPath:sectionNameKeyPath];    

      return [[[NSFetchedResultsController alloc] initWithFetchRequest:request 
                                            managedObjectContext:[self managedContext] 
                                            sectionNameKeyPath:sectionNameKeyPath 
                                            cacheName:[NSString stringWithFormat:@"%@.%@.%@",entityName,sectionNameKeyPath,titleKey]]autorelease];
 }



+(NSFetchRequest *) fetchRequestForEntity:(NSString*)entityName titleKey:(NSString*)titleKey sectionNameKeyPath:(NSString*)sectionNameKeyPath{

    NSFetchRequest *request=[[[NSFetchRequest alloc]init]autorelease];
    request.entity=[NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedContext]];
    NSSortDescriptor* titleSortDescriptor=titleKey?[[NSSortDescriptor alloc ]initWithKey:titleKey ascending:YES]:nil;
    NSSortDescriptor* sectionSortDescriptor=sectionNameKeyPath?[[NSSortDescriptor alloc ]initWithKey:sectionNameKeyPath ascending:YES]:nil;
    NSMutableArray * sortDescriptors=[[NSMutableArray alloc]init];
    if(sectionNameKeyPath){
        [sortDescriptors addObject:sectionSortDescriptor];
        [sectionSortDescriptor release];
    }
    if(titleKey){
        [sortDescriptors addObject:titleSortDescriptor];
        [titleSortDescriptor release];
    }

    request.sortDescriptors=sortDescriptors;
    [sortDescriptors release];

    request.fetchBatchSize=20;
    return request;
}

I don't understand, why If I'm using the same code, my NSFetchResultsController is empty!!!
I tried to fetch data from another entity, but the result is also empty...
Why I'm missing?
Thanks

Was it helpful?

Solution 2

I already solved my problem. Just re-declare the SET method for the NSFetchedResultsController object:

-(void)setFetchedItems:(NSFetchedResultsController *)newFetchedItems{
    [fetchedItems release];
    fetchedItems=[newFetchedItems retain];
    [fetchedItems performFetch:NULL];
}

I guess that's why my object always was empty. Because the object

OTHER TIPS

Simply use the same NSFetchedResultsController. Just change its fetchRequest property to reflect the data you like to fetch. E.g., to fetch all instances of a given entity, use

self.fetchedResultsController.fetchRequest.predicate = nil;
[self.fetchedResultsController performFetch:&error]; 

If you NSFetchedResultsControllerDelegate is set up right, your table view should just update itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top