標籤:
1. 匯入庫檔案CoreData.framework
2. 在iOS的Core Data 中建Data Model檔案 此時有三種選擇
2.1、 選Data Model(如預設名Model.xcdatamodeld)
2.11 再點擊下邊的Add Entity按鈕(修改名為Person)
2.12 再在Attributes中添加(成員變數)儲存變數資訊
2.13 再建立繼承自NSManagedObject的模型Person
2.14 再在模型.h檔案中建立Attributes中添加的成員變數
2.15 在.m檔案中寫@synthesize 成員變數;
2.2、 選Mapping Model(不做介紹)
2.3、 選NSManagedObject subclass(不做介紹)
3. 準備CoreData方法
1.1建立momd(編譯後的副檔名)檔案路徑 1.2在這個檔案中建立Person模型(實體) 1.3建立與實體(模型)對應的資料模型類,此類必須繼承自 NSManagedObject(在上面2中有詳細說明)
NSString *path = [[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"];
在操作之前 別忘記匯入CorData.framework 通過path轉url對象,將momd檔案中的所有的模型(實體)取出放入到NSManagedObjectModel建立的對象中
作用:添加實體的屬性,建立屬性之間的關係
NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]];
2.準備資料庫路徑
NSString *dataPath = [NSString stringWithFormat:@"%@/Documents/myCoreData.db",NSHomeDirectory()];
NSLog(@"dataPath:%@",dataPath);
3.建立持久化儲存協調器 相當於資料庫的連接器
//作用:設定資料存放區的名字,位置,儲存方式和儲存時機
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:objectModel];
4.關聯資料庫
4.1 關聯類別型 在iOS開發中一般都是SQLite (輕量級 一般用於小型行動裝置)4.2配置nil 寫預設即可 4.3資料庫路徑(字串路徑轉url對象)4.4相關模式(操作) nil 4.5錯誤資訊error對象
NSError *error = nil;
NSPersistentStore *store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dataPath] options:nil error:&error];
判斷持久化儲存物件是否為空白,如果為空白說明資料庫建立失敗
if (store == nil) {
NSLog(@"錯誤資訊:%@",error.localizedDescription); //列印報錯資訊
}
5.建立內容物件 取資料(通過CoreData將資料從資料庫取出)
_context = [[NSManagedObjectContext alloc] init];
將內容相關的持久化協調器指定到建立的屬性中 (設定內容物件的協調器)
_context.persistentStoreCoordinator = coordinator;
查
建立尋找類,擷取尋找請求對象,相當於查詢語句 根據實體名字Person得到請求對象
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
通過內容物件執行請求 返回一個數群組類型
NSArray *array = [_context executeFetchRequest:request error:nil];
NSLog(@"array count:%ld",array.count);
通過數組建立數組的類方法 初始化_dataArray成員變數
_dataArray = [NSMutableArray arrayWithArray:array];
6. 增加一個資料模型對象(實體結構對象或實體物件
第一個參數:增加資料對應的模型(增加一個新的資料 根據名字取)
第二個參數:內容物件 註:開發中可以建立多個內容物件管理不同的資料庫,一定保證對應好哪一個內容物件
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:_context];
分別設定名字和年齡
person.name = _nameTextField.text;
person.age = [NSNumber numberWithInteger:[_ageTextField.text integerValue]];
NSError *error = nil;
通過內容物件 調用儲存這個方法 傳入參數error對象的地址 寫入資料庫
BOOL ret = [_context save:&error];
if (ret) { //ret為真 儲存成功 否則失敗
NSLog(@"儲存成功");
將person對象放入到對應的資料 最好重新整理表
[_dataArray addObject:person];
[_tableView reloadData];
}else {
NSLog(@"儲存失敗:%@",error);
}
7. 從資料庫中刪除對象(模型)
取點擊哪個person(點擊哪個cell)
Person *person = _dataArray[_selectedRow];
這裡的刪除操作只是在資料庫中給了一個刪除標記,並沒有實際刪除資料
[_context deleteObject:person];
BOOL ret = [_context save:nil];
if (ret) {
NSLog(@"刪除成功");
從數組中刪除數組元素(person對象)
[_dataArray removeObjectAtIndex:_selectedRow];
重新整理表
[_tableView reloadData];
}else {
NSLog(@"刪除失敗");
}
8. 修改模型資料
擷取請求對象 理解為sqlite語句
NSFetchRequest *request = [[NSFetchRequest alloc] init];
首先通過NSEntityDescription建立實體物件 ,第一個參數實體名字 第二個參數內容物件 然後給請求對象設定實體
[request setEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:_context]];
謂語類 通過謂語指定查詢類型 類似於FMDB where條件 這裡是通過類方法格式化形式建立
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = ‘Aa‘"];
//AND
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = ‘As‘ AND age = 0"];
//OR
//Sql語句 FMDB裡和這裡謂語條件通用 通常提交都和刪除、修改、查詢結合使用
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = ‘As‘ OR age = 28"];
給請求對象設定謂語條件 如果不設定謂語條件會將所有資料修改
[request setPredicate:predicate];
執行請求 返回數組
NSArray *array = [_context executeFetchRequest:request error:nil];
for (Person *person in array) {
person.name = @"不知道";
person.age = [NSNumber numberWithInt:8];
}
儲存(寫回)資料庫,必須要儲存資料庫,否則下次進入應用沒有修改
[_context save:nil];
重新整理表
[_tableView reloadData];
遍曆列印一下
for (Person *person in _dataArray) {
NSLog(@"%@",person.name);
}
9. 查詢模型資料
根據實體名字得到(建立)請求對象
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
建立謂語條件對象
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = ‘不知道‘"];
like 像 屬於一種模糊 開發中經常以什麼名字開頭去查詢 這時候用到like , *代表任意並且B後面不管多少個字元
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like ‘Be*‘"];
以a結尾的查詢
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like ‘*a‘"];
名字包含有a的查詢
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like ‘*a*‘"];
給請求對象設定謂語條件對象
request.predicate = predicate;
執行請求
NSArray *array = [_context executeFetchRequest:request error:nil];
for (Person *person in array) {
NSLog(@"name:%@ age:%@",person.name,person.age);
}
注意:
1.
@dynamic age;
告訴編譯器代碼中用@dynamic修飾的屬性,其getter和setter方法在程式運行時動態建立 或者說動態綁定,以便於編譯器通過編譯(瞭解)
@synthesize age (開發中大多數都用@synthesize )
需要手動建立getter和setter方法的實現
2. 建議手動建立,檔案少
註:如果想用CoreData管理這個Person類建立出來的對象,必須繼承自NSManagedObject,否則CoreData無法操作此類建立出來的對象
在資料模型中,將簡單的資料類型(int float double)轉換成對象 用NSNumber
CoreData之增刪改查