CoreData使用
- 創建步驟流程
- 第一步先創建.xcdatamodeld文件(New File -> iOS -> Core Data ->Data Model)

名字雖然可以任意取,但最好還是取和自己存儲數據庫名字一樣的名字。這樣可讀性更高些。(ps:這個文件就相當于數據庫文件一樣,數據庫文件中可以有多個表,表中可以有各個字段值,這里也可以有多個實體,每個實體有各個鍵值)

通過上面的操作就可以創建一個.xcdatamodeld文件,現在我們點擊這個文件,在最下面找到Add Entity按鈕,進行實體添加。

現在我們添加了一個名字為Entity的實體,這個名字是默認名字,現在我們可以對他進行名字的更改,對它雙擊一下即可修改名字,或者在xcode右邊的信息欄中也可以修改

通過上面操作我們已經創建好了實體的名字,現在我們需要往實體中添加我們需要的鍵值。(ps:相當于數據庫表中的字段),具體操作看圖說話(實體之間的關聯我就不介紹了,有興趣的可以自行搜索資料,個人覺得會了單個實體創建外加查詢就行,關聯也無非是找到關聯的實體中共同的鍵值從而取得另外一個實體對象,我們可以直接先從一個實體取得指定鍵值對應的屬性,再通過屬性值去查詢另一個實體,只是沒關聯的那么方便而已)。

- 第二步創建關聯類來操控CoreData實體對象(選中.xcdatamodeld文件->xcode菜單欄->Edit->Create NSManagedObject Subclass)

選中自己需要關聯的.xcdatamodeld文件名稱,點擊下一步即可。

選中.xcdatamodeld文件中需要關聯的實體對象,點擊下一步然后在選擇存儲目錄即可。

完成后會發現自動生成了實體名稱對應的類和擴展類(Entity.h/.m和Entity+CoreDataProperties.h/.m)

Relationships類似于SQLite的外鍵,定義了在同一個模型中,實體與實體之間的關系。可以定義為對一關系或對多關系,也可以定義單向或雙向的關系,根據需求來確定。如果是對多的關系,默認是使用NSSet集合來存儲模型。
Inverse是兩個實體在Relationships中設置關聯關系后,通過設置inverse為對應的實體,這樣可以從一個實體找到另一個實體,使兩個實體具有雙向的關聯關系。
//
//? ViewController.m
//? CoreDataDemo
// ?
//? Created by pk on 14/10/24.
//? Copyright (c) 2014年 pk. All rights reserved.
//
#import "ViewController.h"
#import <CoreData/CoreData.h>
#import "User.h"
#import "WuGong.h"
@interface ViewController (){
? ? IBOutlet UITextField* _nameField;
? ? IBOutlet UITextField* _scoreField;
? ? //管理者上下文,我們直接調用的類
? ? NSManagedObjectContext* _context;
}
- (IBAction)add:(id)sender;
- (IBAction)fetch:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? _context = [[NSManagedObjectContextalloc] init];
? ? //user模型
? ? NSManagedObjectModel* user = [NSManagedObjectModelmergedModelFromBundles:nil];
? ? //協調者
? ? NSPersistentStoreCoordinator* coordinator = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel:user];
? ? //儲存方式
? ? NSString* path = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents/abcd.db"];
? ? [coordinator addPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:[NSURLfileURLWithPath:path] options:nilerror:nil];
? ? _context.persistentStoreCoordinator = coordinator;
}
- (void)add:(id)sender{
? ? User* user = [NSEntityDescriptioninsertNewObjectForEntityForName:@"User"inManagedObjectContext:_context];
? ? user.name = _nameField.text;
? ? user.score = [NSNumbernumberWithInt:[_scoreField.textintValue]];
? ? user.wugong = [NSEntityDescriptioninsertNewObjectForEntityForName:@"WuGong"inManagedObjectContext:_context];
? ? user.wugong.name =@"葵花寶典";
?? ?
? ? //保存
? ? if ([_contextsave:nil]) {
? ? ? ? NSLog(@"保存成功");
? ? } else {
? ? ? ? NSLog(@"保存失敗");
? ? }
}
- (void)fetch:(id)sender{
? ? //查詢請求
? ? NSFetchRequest* request = [[NSFetchRequestalloc] init];
? ? //關聯要查詢的實體
? ? NSEntityDescription* entity = [NSEntityDescriptionentityForName:@"User"inManagedObjectContext:_context];
? ? [request setEntity:entity];
?? ?
? ? //指定對結果的排序方式
? ? NSSortDescriptor* sortDescriptor = [[NSSortDescriptoralloc] initWithKey:@"name"ascending:YES];
? ? NSArray* sortDescriptions = [[NSArrayalloc] initWithObjects:sortDescriptor,nil];
? ? [request setSortDescriptors:sortDescriptions];
?? ?
? ? //開始查詢
? ? NSArray* array = [_contextexecuteFetchRequest:request error:nil];
? ? for (User* userin array) {
? ? ? ? NSLog(@"%@-%@-%@", user.name, user.score, user.wugong.name);
//? ? ? ? if ([user.name isEqualToString:@"haha"]) {
//? ? ? ? ? ? user.score = @90;
//? ? ? ? ? ? [_context save:nil];
? ? ? ? ? ? [_context deleteObject:user];
//? ? ? ? }
? ? }
}
@end