顯示具有 SQLite 標籤的文章。 顯示所有文章
顯示具有 SQLite 標籤的文章。 顯示所有文章

2012年8月10日 星期五

如何載入預設資料,使用 Core Data

Core Data on iOS 5 Tutorial: How To Preload and Import Existing Data

裡面提到兩種方式

  1. 執行App時,從其他資料匯入到資料戶,例如 JSON, XML, PLIST。
  2. 提供已經載入資料的資料庫。

在這裡我個人是採用第二種方式,

原因是,在邊寫程式邊調整資料庫的結構時,

最後也把資料庫弄好,並且整理好了,

所以在 App 第一次啟動時,

在 Core Data 初始化之前,需要先判斷 .sqlite 檔是否已經存在,

(Core Data 是 基於 SQLite 技術的物件式關連資料庫)

不存在,把之前準備好的資料庫檔 .sqlite (放在 App 的資料夾裡,跟 .h .m 放一起)

複製到 App 的 Document 目錄即可。


沒有採用第一種方式的原因是,

需要判斷是否已經載入之外,

還得另外寫匯入程式,

因為懶惰的關係,所以採用第二種方式唷!


2012年5月14日 星期一

Core Data CRUD 新增、刪除、修改、查詢 (create save delete update select read query)


1. 插入 新增 存檔 create add save

AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [app managedObjectContext];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"entityname" inManagedObjectContext:context];
[newManagedObject setValue:value forKey:@"propertyname"];
NSError *error; if (![context save:&error]) {
 
    // Handle the error…
 
}

2. 查詢 query select read


NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Hero" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]
initWithKey:@"name" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc]
initWithKey:@"secretIdentity" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]
initWithObjects:sortDescriptor1, sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSArray *objecs = [context executeFetchRequest: fetchRequest error:&error];

3. 刪除 delete destroy remove

NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
[context deleteObject:[objecs objectIndex:index];
// Save the context.
NSError *error; if (![context save:&error]) {
 
    // Update to handle the error appropriately.
 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
 
    exit(-1); // Fail
 
}

//屬性

NSManagedObject *managedObject;
NSString *keypath;
NSString *labelString;
NSString *currentValue = [self.managedObject valueForKeyPath:self.keypath];
NSEntityDescription *ed = [self.managedObject entity];
NSDictionary *properties = [ed propertiesByName];
NSAttributeDescription *ad = [properties objectForKey:self.keypath];
NSString *defaultValue = nil;
if (ad != nil)
defaultValue = [ad defaultValue];
//core data relation  查詢 或 修改
NSManagedObject *child = [NSEntityDescription insertNewObjectForEntityForName: @"Person" inManagedObjectContext:thePerson.managedObjectContext];
NSMutableSet *children = [person mutableSetValueForKey:@"children"]; //查詢,可修改
[children addObject:child];
[children removeObject:childToBeRemoved];
[[children managedObjectContext] deleteObject:childToBeRemoved]; //真正的刪除


NSSet *children = [person valueForKey:@"children"]; //查詢,不可修改
for (NSManagedObject *oneChild in children) {
 
    // do something
 
}

補充:

1 是否將圖片存儲到Core Data中,以及NSData如何存儲的一些規則

First, always store your images in a usable format such as PNG or JPEG instead of NSData. This will save you a lot of headaches.

Second, the rule for storing binary data is:


< 100kb store in the same table as the relevant data
< 1mb store in a separate table attached via a relationship to avoid loading unnecessarily
> 1mb store on disk and reference it inside of Core Data

要懂 Core Data 之前, 要先懂 KVO & KVC

引入朋友 CFC 的文章
http://hechien.posterous.com/kvo-01


KVC Key-Value Coding


  • setValue forKey
  • salueForKey

有用過 直譯式語言應該很清楚 KVC
Java 就有如 Reflection 機制


KVO Key-Value Observing

addObserver:forKeyPath:options:context:

MVC 架構 似乎很需要這樣子的通知方式,

有點忘記是否跟 GCD (Grand Central Dispatch ) 有什麼關係。