2011年9月23日 星期五

Lala's Program Note 實作記錄: 27. Set Default Values for Core Data

since: 2011/09/23
update: 2011/09/23

A. 說明:
   1. 需要在一個繼承自 NSManagedObject 的類別裡, 新增一個覆寫 NSManagedObject
       的 awakeFromInsert 方法.

   2. 之前已在 Lala's Program Note 實作記錄: 20. search criteria 中的 B. 建立用來表現
      managed object 的客製物件: 建好了 NoteBook entity 與 NoteArticle entity 的類別.

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

B. 步驟:
   1. 開啟 NoteBook.m 檔案, 新增 method 如下:
      . . . .
@dynamic articles;

//@add
- (void)awakeFromInsert {
    [super awakeFromInsert];
    // Set Default Date
    [self setValue:[NSDate date] forKey:@"dateCreated"];
    [self setValue:[NSDate date] forKey:@"dateUpdate"];
}

@end


   2. 開啟 NoteArticle.m 檔案, 新增 method 如下:
      . . . .
@dynamic notebook;

//@add
- (void)awakeFromInsert {
    [super awakeFromInsert];
    // Set Default Date
    [self setValue:[NSDate date] forKey:@"dateCreated"];
    [self setValue:[NSDate date] forKey:@"dateUpdate"];
}

@end


   3. 開啟 NoteBookViewController.m 檔案, 修改如下:
#import "NoteBookViewController.h"
//@add
#import "ManageNBViewController.h"
#import "ArticleListViewController.h"
//@add
#import "NoteBook.h"

//@add
- (void)insertNoteBookWithName:(NSString *)name subName:(NSString *)subName {
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];

    NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];

    NSManagedObject *newNoteBook = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
   
    [newNoteBook setValue:name forKey:@"name"];
    [newNoteBook setValue:subName forKey:@"subName"];
    //@update: handled in NoteBook.m
    //[newNoteBook setValue:[NSDate date] forKey:@"dateCreated"];
    //[newNoteBook setValue:[NSDate date] forKey:@"dateUpdate"];

    // Save the context
    [self saveContext];
}


   4. 開啟 ArticleListViewController.m 檔案, 修改如下:
#import "ArticleListViewController.h"
//@add
#import "ManageArticleViewController.h"

//@add
#import "ArticleViewController.h"
//@add
#import "NoteArticle.h"

//@add
//- (void)insertArticleWithTitle:(NSString *)title content:(NSString *)content {
- (void)insertArticleWithTitle:(NSString *)title subTitle:(NSString *)subTitle content:(NSString *)content {   

    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
   
    NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
   
    NSManagedObject *newArticle = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
   
    [newArticle setValue:title forKey:@"title"];
    [newArticle setValue:subTitle forKey:@"subTitle"];
    [newArticle setValue:content forKey:@"content"];
   
    //@update: handled in NoteArticle.m
    //Date
    //[newArticle setValue:[NSDate date] forKey:@"dateCreated"];
    //[newArticle setValue:[NSDate date] forKey:@"dateUpdate"];
   
    //[self.noteBook setValue:newArticle forKey:@"articles"];
    // 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-many relationship: property = "articles"; desired type = NSSet; given type = NSManagedObject;
   
    //@use NSSet to handle to-many relationship
    NSMutableSet *articles = [self.noteBook mutableSetValueForKey:@"articles"];
    [articles addObject:newArticle];   
   
    // Save the context
    [self saveContext];
}

沒有留言:

張貼留言

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