CoreData

CoreData就是對(duì)象模型和關(guān)系模型的互轉(zhuǎn),如下圖:

左邊是關(guān)系模型,即數(shù)據(jù)庫(kù);右邊是對(duì)象模型

模型文件:

在Core Data,需要進(jìn)行映射的對(duì)象稱為實(shí)體(entity);下面新建一個(gè)模型文件:

模型文件

程序步驟:

1.新建模型文件

模型文件

2.上代碼

property(nonatomic, strong) NSManagedObjectContext *context;

初始化上下文:

?NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];

? ? NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

? ? // 構(gòu)建SQLite數(shù)據(jù)庫(kù)文件的路徑

? ? NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

? ? NSURL *url = [NSURL fileURLWithPath:[docs stringByAppendingPathComponent:@"person.data"]];

?? ? NSLog(@"%@",url);

? ? // 添加持久化存儲(chǔ)庫(kù),這里使用SQLite作為存儲(chǔ)庫(kù)

? ? NSError*error =nil;

? ? NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error];

? ? if(store ==nil) {// 直接拋異常

? ? ? ? [NSException raise:@"添加數(shù)據(jù)庫(kù)錯(cuò)誤" format:@"%@", [error localizedDescription]];

? ? }

? ? // 初始化上下文,設(shè)置persistentStoreCoordinator屬性

? ? NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:0];

? ? self.context= context;

? ? context.persistentStoreCoordinator = psc;

插入數(shù)據(jù):

-(void)addinto{

? ? // 傳入上下文,創(chuàng)建一個(gè)Person實(shí)體對(duì)象

? ? NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];

? ? // 設(shè)置Person的簡(jiǎn)單屬性

? ? [personsetValue:@"MJ"forKey:@"name"];

? ? [personsetValue:[NSNumber numberWithInt:27] forKey:@"age"];

? ? // 傳入上下文,創(chuàng)建一個(gè)Card實(shí)體對(duì)象

? ? NSManagedObject *card = [NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:self.context];

? ? [cardsetValue:@"4414241933432" forKey:@"no"];

? ? // 設(shè)置Person和Card之間的關(guān)聯(lián)關(guān)系

? ? [personsetValue:cardforKey:@"card"];

? ? // 利用上下文對(duì)象,將數(shù)據(jù)同步到持久化存儲(chǔ)庫(kù)

? ? NSError*error =nil;

? ? BOOLsuccess = [self.contextsave:&error];

? ? if(!success) {

? ? ? ? [NSException raise:@"訪問數(shù)據(jù)庫(kù)錯(cuò)誤" format:@"%@", [error localizedDescription]];

? ? }

}

查詢:

-(void)query{

? ? // 初始化一個(gè)查詢請(qǐng)求

? ? NSFetchRequest *request = [[NSFetchRequest alloc] init];

? ? // 設(shè)置要查詢的實(shí)體

? ? request.entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.context];

? ? // 設(shè)置排序(按照age降序)

? ? NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];

? ? request.sortDescriptors = [NSArray arrayWithObject:sort];

? ? // 執(zhí)行請(qǐng)求

? ? NSError*error =nil;

? ? NSArray*objs = [self.contextexecuteFetchRequest:requesterror:&error];

? ? if(error) {

? ? ? ? [NSException raise:@"查詢錯(cuò)誤" format:@"%@", [error localizedDescription]];

? ? }

? ? // 遍歷數(shù)據(jù)

? ? for (NSManagedObject *obj in objs) {

? ? ? ? NSLog(@"name=%@", [objvalueForKey:@"name"]);

? ? ? ? ? ? ? }

}

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

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

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