CoreData筆記

? ? ? ?CoreData提供了對象-關(guān)系映射(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ù)比較復雜時,可能需要建多個表,多表關(guān)聯(lián):需要設(shè)置relationships

主要設(shè)置:

Type:To One 一對一 ToMany ?一對多

Delele Rule:

//NSNoActionDeleteRule--No Action

無任何刪除動作。

如:A表中的關(guān)系字段值與B表中的鍵值相對應,當刪除B表中的數(shù)據(jù)時,A中的關(guān)系字段值保持不變。

//NSNullifyDeleteRule--Nullify

空刪除規(guī)則。

如:A表中的關(guān)系字段值與B表中的鍵值相對應,當刪除B表中的數(shù)據(jù)時,A中的關(guān)系字段值變?yōu)榱耍危眨蹋獭?/p>

//NSCascadeDeleteRule --Cascade

聯(lián)動刪除規(guī)則。

如:當把A表中的關(guān)系設(shè)置為聯(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • //聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄 一、CoreData的簡單使用 1.什么是Core...
    石虎132閱讀 256評論 0 9
  • #import "ViewController.h"#import#import "Teacher.h" #imp...
    艾克12138閱讀 403評論 0 0
  • 本人ios初學者,為自己學習方便,復制各位大神的學習性文章放在自己簡書里,僅作為自己學習方便使用,如果作者疑此行為...
    bu再等閱讀 722評論 0 0
  • 沙盒 Plist Preference偏好設(shè)置 NSKeyedArchiver歸檔 / NSKeyedUnarch...
    追風者366閱讀 3,588評論 0 6
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 3,988評論 0 11

友情鏈接更多精彩內(nèi)容