Core Data是iOS中十分重要的一种数据持久化的方式,之前存储数据一直使用的FMDB,很少使用Core Data;但是,还是有必要来了解一下这个高大上的数据持久化的方式的.
由于笔者对此认知有限,不正确的地方还请指正,感谢!

开始之前,我们先来了解一下这几个对象:

NSManagedObjectModel
这个是应用程序的数据模型,这个模型包含实体(Entity),属性(Property),读取请求(Fetch Request)等

NSManagedObjectContext
参与对数据对象进行各种操作的全过程,并检测数据对象的变化,以提供对undo/redo的支持及更新绑定到数据的UI
NSManagedObject
数据记录
NSEntityDescription
数据实体对象
NSPersistentStoreCoordinator
主要处理底层对数据的读取与写入操作,一般我们不需要与他接触;
另外:.xcdatamodeld文件编译后为.momd或者.mom文件

一. 新建工程,并启用Core Data

想要使用Core Data,需要在新建工程的时候勾选(当然也可以手动创建),即在输入工程名称的界面勾选Use Core Data ,如下图:

勾选Use Core Data

这样新建的工程,Xcode会帮我们添加一些与Core Data相关的内容,你会发现,在项目的左边列表,多了一个以xcdatamodeld为后缀名的文件,这就是我们的Data Model文件:

xcdatamodeld文件

这个是默认生成的,当然我们也可以手动添加,和新建其他文件一样:右键-->New File...选择左侧Core Data --> Data Model

手动创建Data Model

Next,输入名称,Create 即可!
和未勾选Core Data的工程的区别还有,就是在AppDelegate中,其中.h文件中多了以下内容:

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@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

在.m文件中,增加了相关的方法的实现,一般在文件的最下面可以看到!

- (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.artup.LZCoreData" 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:@"LZCoreData" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    // The persistent store coordinator for the application. This implementation creates and returns 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:@"LZCoreData.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] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_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();
        }
    }
}

二 . 添加数据模型的可视化操作

选择LZCoreData.xcdatamodeld文件,所有的可视化操作都是在这个文件进行的;

1.添加一个实体(Entity)

点击下方的+按钮(Add Entity),输入你要建立的实体名称,这里是PeopleEntity

添加实体PeopleEntity

2. 添加一个属性

属性主要显示在中间的Attributes一栏,点击其左下的+,可以新加一个属性,输入名称,并选择一个类型,这里定义一个String类型的name属性:

添加属性Attributes

注意:这里的属性名称首字母必须小写,否则会有如下提示:

属性名称不合格

这里的数据类型中,有一个比较特殊:Transformble,对应于OC中的id类型;
同样方式,再添加age和sex属性;
然后,再添加一个实体ManEntity,属性设置如下:

添加实体ManEntity

3. 建立联系

这里已建立PeopleEntity和ManEntity之间的联系为例,
选择PeopleEntity实体,在中间的Relationships一栏,点击+,添加一个联系,起一个名称,并在Destination中选择ManEntity:

添加与ManEntity的联系

这里的作用相当于,在实体PeopleEntity中添加了一个类型为ManEntity,名称为manRelationship的属性;
Apple官方建议我们在建立一个目标关系后,要建立一个返回关系;即在ManEntity中建立一个和PeopleEntity的联系;

添加与PeopleEntity的联系

选中一个联系后,在右侧可以设置联系的一些属性:

联系的一些设置

例如Delete Rule(删除规则),Type(联系类型,一对多,一对一)等;
这样,我们的数据模型就建立好了,接下来的事情,就是如何去使用了.

三. 使用数据模型

接下来的操作主要就是写代码了,来到我们的ViewController.m文件,导入AppDelegate.h头文件,我们需要用到里面关于Core Data的一些内容;新建一个方法test1,添加以下代码:

//获取代理
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    //获取context
    NSManagedObjectContext *context = [delegate managedObjectContext];
    
    //获取PeopleEntity实体
    NSManagedObject *people = [NSEntityDescription insertNewObjectForEntityForName:@"PeopleEntity" inManagedObjectContext:context];
    
    //设置属性内容
    [people setValue:@"流火绯瞳" forKey:@"name"];
    [people setValue:@26 forKey:@"age"];
    [people setValue:@0 forKey:@"sex"];
    
    //获取ManEntit实体
    NSManagedObject *man = [NSEntityDescription insertNewObjectForEntityForName:@"ManEntity" inManagedObjectContext:context];
    
    [man setValue:@178.0 forKey:@"height"];
    [man setValue:@60.0 forKey:@"weight"];
    [man setValue:@"张三" forKey:@"name"];
    [man setValue:people forKey:@"peopleRelationship"];
    
    [people setValue:man forKey:@"manRelationship"];
    
    NSError *error;
    //保存更改
    if ([context save:&error]) {
        NSLog(@"保存成功");
    } else {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }
    
    //查询实体
    //创建一个查询请求
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    //获取要查询的实体
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"PeopleEntity" inManagedObjectContext:context];
    //添加到查询请求
    [fetchRequest setEntity:entity];
    //开始查询并获取结果
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    
    NSLog(@"输出查询结果");
    for (NSManagedObject *info in fetchedObjects) {
        
        NSLog(@"Name: %@", [info valueForKey:@"name"]);
        NSLog(@"age: %@", [info valueForKey:@"age"]);
        NSLog(@"sex: %@", [info valueForKey:@"sex"]);
        NSLog(@"-----------------------------------");
        
        NSManagedObject *man1 = [info valueForKey:@"manRelationship"];
        NSLog(@"Name: %@", [man1 valueForKey:@"name"]);
        NSLog(@"weight: %@", [man1 valueForKey:@"weight"]);
        NSLog(@"height: %@", [man1 valueForKey:@"height"]);
        NSLog(@"==========================================");
    }

代码中我简单的加入了注释,这里只是为了测试一下创建的数据模型是否可用;
所以,只是使用了添加数据和查询数据的方法,然后运行程序,可以在控制台看到如下输出:

2016-05-26 15:37:25.496 LZCoreData[5654:567413] 保存成功
2016-05-26 15:37:25.497 LZCoreData[5654:567413] 输出查询结果
2016-05-26 15:37:25.498 LZCoreData[5654:567413] Name: 流火绯瞳
2016-05-26 15:37:25.498 LZCoreData[5654:567413] age: 26
2016-05-26 15:37:25.498 LZCoreData[5654:567413] sex: 0
2016-05-26 15:37:25.498 LZCoreData[5654:567413] -----------------------------------
2016-05-26 15:37:25.498 LZCoreData[5654:567413] Name: 张三
2016-05-26 15:37:25.499 LZCoreData[5654:567413] weight: 60
2016-05-26 15:37:25.499 LZCoreData[5654:567413] height: 178
2016-05-26 15:37:25.501 LZCoreData[5654:567413] ==========================================

表示我们成功的保存了数据,并查询了出来.
你可能会发现,这里我在设置属性值和取值的时候,都是使用的KVC的模式,这样在使用时是相当不方便的,能不能像其他的Model那样,利用.的形式来赋值取值呢?答案是肯定的.


流火绯瞳
27 声望1 粉丝