2011年9月21日 星期三

Lala's Program Note 實作記錄: 22. 瀏覽我的最愛之一

since: 2011/09/21
update: 2011/09/21


A.
之前, 在 Lala's Program Note 實作記錄: 2.標籤列架構, 中的 F. 增加 Favorite
   (我的最愛) 頁籤
, 已新增了 FavoriteViewController

B. 為瀏覽我的最愛(Favorites) 加上 NSFetchedResultsController:
    (
NSFetchedResultsController 是設計用來讓 Core DataTable view 整合的類別.)
   1. 修改 FavoriteViewController.h 如下:
#import <UIKit/UIKit.h>
//@add
#import <CoreData/CoreData.h>

//@interface FavoriteViewController : UITableViewController
@interface FavoriteViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
    //@add
    NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
}

//@add
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

@end

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

   2. 修改 FavoriteViewController.m 如下:

//@add
@synthesize fetchedResultsController, managedObjectContext;

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


#pragma mark -
#pragma mark Fetched results controller
// Do it in the accessor for the fetchedResultsController member
- (NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }
   
    // Create the fetch request for the entity
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
   
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"NoteArticle" 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:@"title" 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:@"NoteArticle"];
   
    //@add predicate
    //also: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"favorite == %@", [NSNumber numberWithBool:YES]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"favorite == YES"];
    [aFetchedResultsController.fetchRequest setPredicate:predicate];
   
    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;
}

  

沒有留言:

張貼留言

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