Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for fetchedResultsController code using magicalrecord?

I'm moving my app's core data over to Magical Record. For updating a UITableView, i previously had this code:

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entry" inManagedObjectContext:[CoreDataStore mainStore].context];
    [fetchRequest setEntity:entity];

    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Use the sectionIdentifier property to group into sections.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[CoreDataStore mainStore].context sectionNameKeyPath:@"sectionIdentifier" cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    self.fetchedResultsController.delegate = self;

    return _fetchedResultsController;
}

Should I keep this code in place, or should it work differently using magical record?

like image 847
Andrew Avatar asked Jul 20 '12 13:07

Andrew


1 Answers

You can use like this:

- (NSFetchedResultsController *)fetchedResultsController {
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    _fetchedResultsController = [NSManagedObject fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"date" ascending:NO delegate:self];

    return _fetchedResultsController;
}

You have to replace NSManagedObject with your subclass.

You can see other options here (bottom of header).

like image 146
gabriel_rinaldi Avatar answered Oct 10 '22 13:10

gabriel_rinaldi