? ? ? ?CoreData提供了對象-關系映射(ORM)的功能,能夠?qū)C對象轉(zhuǎn)化成數(shù)據(jù)掰烟,保存在SQLit在e數(shù)據(jù)庫文件或者XML中毡们,也能夠?qū)⒈4嬖跀?shù)據(jù)庫中的數(shù)據(jù)還原成OC對象。
CoreData常用的類中如下:
1.NSManagedObjectContext 被管理對象上下文
2.NSPersistentStoreCoordinator 持久化存儲協(xié)調(diào)器
3.NSManagedObjectModel ?被管理數(shù)據(jù)模型
4.NSManagedObject 被管理數(shù)據(jù)記錄
5.NSFetchRequest 獲取數(shù)據(jù)的請求
6. NSEntityDescription 實體結(jié)構(gòu)
?類之間的結(jié)構(gòu)如下圖:
相當于表格結(jié)構(gòu)
插入數(shù)據(jù)
-(void)insert
{
? ? ? ? ? ?Student *stu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" ? ? ? ? ? ? ?inManagedObjectContext:_context];
? ? ? ? ? ?stu.name = @"jyq";
? ? ? ? ? ?stu.hight = @170;
? ? ? ? ? ?stu.birthday = [NSDate date];
? ? ? ? ? ?NSError *error = nil;
? ? ? ? ? ?[_context save:&error];
? ? ? ? ? ?if (error) {
? ? ? ? ? ? ? ? ? ? ?NSLog(@"insert error:%@",error);
? ? ? ? ? }
}
更新數(shù)據(jù)庫
-(void)update
{
? ? ? ? ? ?NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
? ? ? ? ? ?NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@",@"laoliu"];
? ? ? ? ? ?request.predicate = pre;
? ? ? ? ? ?NSArray *students = [_context executeFetchRequest:request error:nil];
? ? ? ? ? for (Stuentd *stu in students) {
? ? ? ? ? ? ? ? ? ?stu.hight = @173;
? ? ? ? ? }
? ? ? ? ? [_context save:nil];
}
刪除數(shù)據(jù)
-(void)delete
{
? ? ? ? ?NSFetchRequest *reqest = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
? ? ? ? ?NSPredicate *pre = [NSPredicate predicateWithFormat:@"name=%@",@"laoliu"];
? ? ? ? ? reqest.predicate=pre;
? ? ? ? NSArray *students = [_context executeFetchRequest:reqest error:nil];
? ? ? ? ?for (Student *stu in students) {
? ? ? ? ? ? ? ? [_context deleteObject:stu];
? ? ? ? ?}
? ? ? ? [_context save:nil];
}
查詢數(shù)據(jù)庫
-(void)fetch?
{
? ? ? ? ? NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
? ? ? ? ? NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"hight" ascending:NO];
? ? ? ? ? ?request.sortDescriptors = @[sort];
? ? ? ? ? NSPredicate *pre = [NSPredicate predicateWithFormat:@"hight>175"];
? ? ? ? ? request.predicate = pre;
? ? ? ? ? NSError *error =nil;
? ? ? ? ?NSArray *students = [_context executeFetchRequest:request error:&error];
? ? ? ? ?if (error) {
? ? ? ? ? ? ? ?NSLog(@"fetchResult error:%@",error);
? ? ? ? ?}
? ? ? ? ?for (Student*stu in ?students) {
? ? ? ? ? ? ? ? ? NSLog(@"stu:name:%@,hight:%@,brt:%@",stu.name,stu.hight,stu.birthday );
? ? ? ? ?}
}
當數(shù)據(jù)比較復雜時春宣,可能需要建多個表酵颁,多表關聯(lián):需要設置relationships
主要設置:
Type:To One 一對一 ToMany ?一對多
Delele Rule:
//NSNoActionDeleteRule--No Action
無任何刪除動作。
如:A表中的關系字段值與B表中的鍵值相對應信认,當刪除B表中的數(shù)據(jù)時材义,A中的關系字段值保持不變。
//NSNullifyDeleteRule--Nullify
空刪除規(guī)則嫁赏。
如:A表中的關系字段值與B表中的鍵值相對應其掂,當刪除B表中的數(shù)據(jù)時,A中的關系字段值變?yōu)榱耍危眨蹋獭?/p>
//NSCascadeDeleteRule --Cascade
聯(lián)動刪除規(guī)則潦蝇。
如:當把A表中的關系設置為聯(lián)動刪除時款熬,哪么A表在刪除數(shù)據(jù)時候,將對聯(lián)動表的數(shù)據(jù)也進行刪除攘乒。
//NSDenyDeleteRule --Deny
依賴刪除規(guī)則贤牛。
//如刪除部門時必須確保該部門員工表中的數(shù)據(jù)為空或轉(zhuǎn)到別處,否則刪除失敗则酝。
?CoreData? 多線程問題
? ? 在多線程編程環(huán)境下殉簸,容易出現(xiàn)每個上下文數(shù)據(jù)不一致問題,最好一個線程一個上下文沽讹,如何需要修改數(shù)據(jù)般卑,盡量有一個線程的上下文去修改,然后通知其他上下文進行修改爽雄,通知方法:上下文修改時(save 方法調(diào)用)時系統(tǒng)會發(fā)送NSManagedObjectContextDidSaveNotification通知蝠检,我們許監(jiān)聽這個通知,然后修改上下文如:[_context mergeChangesFromContextDidSaveNotification:notice];
參考資料:
http://www.cppblog.com/ipzyh/articles/CoreData.html
系列文章:http://blog.csdn.net/jasonblog/article/details/8528850