2011年8月29日 星期一

Lala's Program Note 實作記錄: 4. NSFetchedResultsController 之一

since: 2011/08/29
update: 2011/08/29


參考書籍:
Pro Core Data for iOS: Data Access and Persistence Engine for iPhone, iPad, and iPod touch

A. 說明:
   1. NSFetchedResultsController 是設計用來讓 Core DataTable view 整合的類別.


B. 為瀏覽筆記本(NoteBook) 加上 NSFetchedResultsController:
   1. 修改 NoteBookViewController.h 如下:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface NoteBookViewController : UITableViewController {
@interface NoteBookViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
    NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
}
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@end

----------------------------------------------------------------------------------------------------------

   2. 修改 NoteBookViewController.m 如下:

@synthesize fetchedResultsController, managedObjectContext;

- (void)dealloc {
    [fetchedResultsController release];
    [managedObjectContext release];
    [super dealloc];
}


----------------------------------------------------------------------------------------------------------

   3. 在 NoteBookViewController.m 建立 Fetched Results Controller
      // Do it in the accessor for the fetchedResultsController member
#pragma mark -
#pragma mark Fetched results controller

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

    // Create the fetch request for the entity
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"NoteBook" inManagedObjectContext:self.managedObjectContext];

    [fetchRequest setEntity:entity];

    // Set the batch size
    [fetchRequest setFetchBatchSize:10];

    // sort by noteBook name, ascending
    // * with Fetched Results Controller, you must have at least one sort descriptor.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create the fetched results controller using the fetch request we just created,
    // and with the managed object context member,
    // and set this controller to be the delegate

    // * we pass nil for the sectionNameKeyPath parameter,
    //   because we have no sections.

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"NoteBook"];
   
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    // Clean up
    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    // Fetch the results into the fetched results controller
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        //TODO
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
   
    return fetchedResultsController;
}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。