coreData使用

本人ios初學者,為自己學習方便,復制各位大神的學習性文章放在自己簡書里,僅作為自己學習方便使用,如果作者疑此行為侵權,請隨時聯系本人刪除,如有共同學習者復制此文章,請注明原出處

http://www.th7.cn/Program/IOS/201506/490903.shtml

Core Date是ios3.0后引入的數據持久化解決方案,它是是蘋果官方推薦使用的,不需要借助第三方框架。Core Date實際上是對SQLite的封裝,提供了更高級的持久化方式。在對數據庫操作時,不需要使用sql語句,也就意味著即使不懂sql語句,也可以操作數據庫中的數據。  在各類應用開發中使用數據庫操作時通常都會用到 (ORM) “對象關系映射”,Core Data就是這樣的一種模式。ORM是將關系數據庫中的表,轉化為程序中的對象,但實際上是對數據中的數據進行操作。  在使用Core Data進?行數據庫存取并不需要手動創建數據庫,創建數據庫的過程完全由Core Data框架自動完成,開發者需要做的就是把模型創建起來,具體數據庫的創建不需要管。簡單點說,Core Data實際上是將數據庫的創建、表的創建、對象和表的轉換等操作封裝起來,極大的簡化了我們的操作。  Core Date與SQLite相比較,SQLite比較原始,操作比較復雜,使用的是C的函數對數據庫進行操作,但是SQLite可控性更強,并且能夠跨平臺。  下面,讓我們一起來學習一下Core Data的簡單使用。一、使用Core Data,添加實體和模型  在創建項目的時候可以選擇使用Core Data,項目創建成功后,會在AppDelegate類中自動添加相關代碼,此外,還會自動生成一個數據模型文件JRCoreData.xcdatamodeld AppDelegate.h//? AppDelegate.h//? JRCoreData////? Created by jerei on 15-6-24.//? Copyright (c) 2015年 jerehedu. All rights reserved.//#import#import@interface AppDelegate : UIResponder@property (strong, nonatomic) UIWindow *window;@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;- (void)saveContext;- (NSURL *)applicationDocumentsDirectory;@end

AppDelegate.m

//? AppDelegate.m//? JRCoreData////? Created by jerei on 15-6-24.//? Copyright (c) 2015年 jerehedu. All rights reserved.//#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {? ? // Override point for customization after application launch.? ? return YES;}- (void)applicationWillResignActive:(UIApplication *)application {? ? // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.? ? // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {? ? // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.? ? // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {? ? // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {? ? // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {? ? // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.? ? // Saves changes in the application's managed object context before the application terminates.? ? [self saveContext];}#pragma mark - Core Data stack@synthesize managedObjectContext = _managedObjectContext;@synthesize managedObjectModel = _managedObjectModel;@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;- (NSURL *)applicationDocumentsDirectory {? ? // The directory the application uses to store the Core Data store file. This code uses a directory named "com.jerehedu.JRCoreData" in the application's documents directory.? ? return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];}- (NSManagedObjectModel *)managedObjectModel {? ? // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.? ? if (_managedObjectModel != nil) {? ? ? ? return _managedObjectModel;? ? }? ? NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"JRCoreData" withExtension:@"momd"];? ? _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];? ? return _managedObjectModel;}- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {? ? // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.? ? if (_persistentStoreCoordinator != nil) {? ? ? ? return _persistentStoreCoordinator;? ? }? ? ? ? // Create the coordinator and store? ? ? ? _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];? ? NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"JRCoreData.sqlite"];? ? NSError *error = nil;? ? NSString *failureReason = @"There was an error creating or loading the application's saved data.";? ? if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {? ? ? ? // Report any error we got.? ? ? ? NSMutableDictionary *dict = [NSMutableDictionary dictionary];? ? ? ? dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";? ? ? ? dict[NSLocalizedFailureReasonErrorKey] = failureReason;? ? ? ? dict[NSUnderlyingErrorKey] = error;? ? ? ? error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];? ? ? ? // Replace this with code to handle the error appropriately.? ? ? ? // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.? ? ? ? NSLog(@"Unresolved error %@, %@", error, [error userInfo]);? ? ? ? abort();? ? }? ? ? ? return _persistentStoreCoordinator;}



- (NSManagedObjectContext *)managedObjectContext {? ? // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)? ? if (_managedObjectContext != nil) {? ? ? ? return _managedObjectContext;? ? }? ? ? ? NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];? ? if (!coordinator) {? ? ? ? return nil;? ? }? ? _managedObjectContext = [[NSManagedObjectContext alloc] init];? ? [_managedObjectContext setPersistentStoreCoordinator:coordinator];? ? return _managedObjectContext;}#pragma mark - Core Data Saving support- (void)saveContext {? ? NSManagedObjectContext *managedObjectContext = self.managedObjectContext;? ? if (managedObjectContext != nil) {? ? ? ? NSError *error = nil;? ? ? ? if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {? ? ? ? ? ? // Replace this implementation with code to handle the error appropriately.? ? ? ? ? ? // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.? ? ? ? ? ? NSLog(@"Unresolved error %@, %@", error, [error userInfo]);? ? ? ? ? ? abort();? ? ? ? }? ? }}@end

如果項目在創建的時候沒有選擇使用Core Data,但是在后面需要使用,那么需要手動的添加AppDelegate中的相關代碼。此外,還需要手動添加一個Data Model文件

創建Data Model文件時需要注意,文件名稱要與AppDelegate.m中managedObjectModel方法中提到的文件名稱相匹配。

有了Data Model文件后,就可以在里面添加實體和關系,實際上就是向數據庫中添加表格和建立表格之間的關聯。添加實體如圖所示:

每個學生有一個所在的班級,每個班級中有多個學生,因此,學生和班級之間可以建立關系。建立關系如圖所示:

建立關系之后,可以切換顯示的樣式,以圖表的方式查看實體之間的關系,如圖所示:

完成上述步驟,數據庫中表格的創建就已經完成,和使用SQLite比較,省略了sql語句以及調用C函數操作數據庫的步驟,另外,在創建實體的時候不需要設置主鍵,實體對象的屬性的類型是OC的類型,實體中其他實體對象類型是通過建立關系添加的。

創建好實體后,可以通過添加NSManagedObject subclass文件,系統可以自動添加實體對應的數據模型類,如圖所示:

二、通過代碼實現數據庫的操作

1、 向學生表中插入一條數據

在使用Core Data的時候,AppDelegate中添加了NSManagedObjectContext對象,需要獲得這個管理對象的上下文來進行操作。在操作的過程中,需要得到NSManagedObject實體,然后通過kvc設置實體的屬性值,最后通過上下文調用save方法保存數據。

- (void)insert {? ? ? ? AppDelegate *delegate = [[UIApplication sharedApplication] delegate];? ? ? ? //1. 獲得context? ? NSManagedObjectContext *context = delegate.managedObjectContext;? ? //2. 找到實體結構,并生成一個實體對象? ? /*? ? NSEntityDescription實體描述,也就是表的結構? ? 參數1:表名字? ? 參數2:實例化的對象由誰來管理,就是context? ? */? ? NSManagedObject *stu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];? ? ? ? NSManagedObject *class1 = [NSEntityDescription insertNewObjectForEntityForName:@"Classes" inManagedObjectContext:context];? ? [class1 setValue:[NSNumber numberWithInt:1] forKey:@"c_id"];? ? [class1 setValue:@"一班" forKey:@"c_name"];? ? ? ? //3. 設置實體屬性值? ? [stu setValue:[NSNumber numberWithInt:1] forKey:@"s_id"];? ? [stu setValue:@"jerehedu" forKey:@"s_name"];? ? [stu setValue:class1 forKey:@"s_class"];? ? ? ? //4. 調用context,保存實體,如果沒有成功,返回錯誤信息? ? NSError *error;? ? if ([context save:&error]) {? ? ? ? NSLog(@"save ok");? ? }? ? else? ? {? ? ? ? NSLog(@"%@",error);? ? }}

2、查詢學生表中全部數據

查詢與插入數據操作類似,但是多了構造查詢對象的步驟,查詢得到結果集是一個數組,遍歷數組,可以取出查詢數據。

- (void)selectAll {? ? AppDelegate *delegate = [[UIApplication sharedApplication] delegate];? ? ? ? NSManagedObjectContext *context = delegate.managedObjectContext;? ? ? ? NSEntityDescription *stu = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];? ? ? ? //構造查詢對象? ? NSFetchRequest *request = [[NSFetchRequest alloc] init];? ? [request setEntity:stu];? ? ? ? //執行查詢,返回結果集? ? NSArray *resultAry = [context executeFetchRequest:request error:nil];? ? ? ? //遍歷結果集? ? for (NSManagedObject *enity in resultAry) {? ? ? ? NSLog(@"id=%i name=%@ class=%@",[[enity valueForKey:@"s_id"] intValue],[enity valueForKey:@"s_name"],[[enity valueForKey:@"s_class"] valueForKey:@"c_name"]);? ? }}

3、查詢指定條件的學生信息,并更新

指定條件的查詢除了需要構造查詢對象,還需要把查詢的條件用謂詞表示。然后遍歷查詢結果數組中的數據,進行更行,并保存。

- (void)update{? ? //? ? 更新 (從數據庫找到-->更新)? ? AppDelegate *delegate = [[UIApplication sharedApplication] delegate];? ? NSManagedObjectContext *context = delegate.managedObjectContext;? ? ? ? NSEntityDescription *stu = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];? ? ? ? NSFetchRequest *request = [NSFetchRequest new];? ? [request setEntity:stu];? ? ? ? //構造查詢條件,相當于where子句? ? NSPredicate *predicate = [NSPredicate predicateWithFormat:@"s_id=%i",1];? ? ? ? //把查詢條件放進去? ? [request setPredicate:predicate];? ? ? ? //執行查詢? ? NSArray *studentAry = [context executeFetchRequest:request error:nil];? ? if (studentAry.count>0)? ? {? ? ? ? //更新里面的值? ? ? ? NSManagedObject *obj = studentAry[0];? ? ? ? [obj setValue:@"apple" forKey:@"s_name"];? ? }? ? [context save:nil];? ? ? ? //顯示? ? [self selectAll];}

4、刪除指定條件的學生信息

刪除之前首先需要根據條件進行查詢,查詢到數據后刪除,并保存。

- (void)delete{? ? //刪除 先找到,然后刪除? ? AppDelegate *delegate = [[UIApplication sharedApplication] delegate];? ? NSManagedObjectContext *context = delegate.managedObjectContext;? ? ? ? NSEntityDescription *stu = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];? ? ? ? NSFetchRequest *request = [NSFetchRequest new];? ? [request setEntity:stu];? ? ? ? //構造查詢條件,相當于where子句? ? NSPredicate *predicate = [NSPredicate predicateWithFormat:@"s_id=%i",1];? ? ? ? //把查詢條件放進去? ? [request setPredicate:predicate];? ? //執行查詢? ? NSManagedObject *obj = [[context executeFetchRequest:request error:nil] lastObject];? ? //刪除? ? if (obj) {? ? ? ? [context deleteObject:obj];? ? ? ? [context save:nil];? ? }? ? ? ? [self selectAll];}

三、小結

Core Data是蘋果官方推薦使用的數據持久化方式,在使用的過程中,不需要導入數據庫框架,也不需要使用sql語句操作數據庫,完全是按照面向對象的思想,使用實體模型來操作數據庫。在使用的過程中需要注意的是,如果模型發生了變化,可以選擇重新生成實體類文件,但是自動生成的數據庫并不會自動更新,需要考慮重新生成數據庫,并把之前數據庫中數據進行移植。Core Data能夠簡化操作,但是它不支持跨平臺使用,如果想實現跨平臺,就需要使用SQLite來進行數據持久化。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容