2011年9月24日 星期六

Lala's Program Note 實作記錄: 29. Error Handing: Core Data Operational

since: 2011/09/24
update: 2011/09/24


A. 新增一個共用的類別, 來處理共同的事情.
   1. 專案目錄 > New File >
   2. iOS > Cocoa Touch > Objective-C class > Next >
   3. Subclass 選擇: NSObject > Next
   4. Save As: Common.m

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

B. 開啟 Common.h 檔案修改如下:
//@add
+ (void)showCoreDataError;

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

C. 開啟 Common.m 檔案修改如下:
//@add
+ (void)showCoreDataError {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Lala's Program Note can't continue.\nPress the Home button to close it." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
   
    [alert show];
    [alert release];
}

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

D. 套用到 AppDelegate 裡:
   開啟 Lala_s_Program_NoteAppDelegate.m 檔案, 修改如下:
   //@add
   #import "Common.h"
      . . . .
- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil)
    {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
        {
            . . . .
            //NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            //abort();
            //@update
            [Common showCoreDataError];
        }
    }
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    . . . .
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {
        . . . .
        //NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //abort();
        //@update
        [Common showCoreDataError];
    }   
   
    return __persistentStoreCoordinator;
}

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

E. 套用到 SearchViewController 裡:
   開啟 SearchViewController.m 檔案, 修改如下:
   //@add
   #import "Common.h"

//@add
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
    . . . .
    if (![[self fetchedResultsController] performFetch:&error])
    {
        // Handle error
        //NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //exit(-1);  // Fail
        //@add
        [Common showCoreDataError];
    }
    . . . .
}

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

F. 套用到 FavoriteViewController 裡:
   開啟 FavoriteViewController.m 檔案, 修改如下:
   //@add
   #import "Common.h"

//@add
- (void)saveContext {
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
   
    NSError *error = nil;
    if (![context save:&error]) {
        //NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //abort();
        //@add
        [Common showCoreDataError];
    }
}


// Do it in the accessor for the fetchedResultsController member
- (NSFetchedResultsController *)fetchedResultsController {
    . . . .
    // Fetch the results into the fetched results controller
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        //NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //abort();
        //@add
        [Common showCoreDataError];
    }
   
    return fetchedResultsController;
}


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

G. 套用到 NoteBookViewController 裡:
   開啟 NoteBookViewController.m 檔案, 修改如下:
   //@add
   #import "Common.h"

// Creating the Fetched Results Controller:
// Do it in the accessor for the fetchedResultsController member
- (NSFetchedResultsController *)fetchedResultsController {
    . . . .
    // Fetch the results into the fetched results controller
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        //NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //abort();
        //@add
        [Common showCoreDataError];
    }
   
    return fetchedResultsController;
}

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

H. 套用到 ArticleListViewController 裡:
   開啟 ArticleListViewController.m 檔案, 修改如下:
   //@add
   #import "Common.h"

// Creating the Fetched Results Controller:
// Do it in the accessor for the fetchedResultsController member
- (NSFetchedResultsController *)fetchedResultsController {
    . . . .
    // Fetch the results into the fetched results controller
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        //NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //abort();
        //@add
        [Common showCoreDataError];
    }
   
    return fetchedResultsController;
}

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

I. 測試:
   1. 先執行一次 App, 關閉後到 data model 裡, 對 NoteBook entity 新增一個 attribute,
       然後再執行 App, 因為 data model 已經不相符合了, persistent store coordinator
       就無法開啓 data store 而發生 error 了.

   2. 結果圖:

沒有留言:

張貼留言

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