1.简介
1)coreData提供了对象持久化管理,不需要关心数据的内部存储,只需要关心对象的增删查改.
FMDB是在对sqlite的轻量级封装,方便操作数据库;CoreData直接与Sqlite交互,避免开发者使用原本的SQL语句,并且使用图形界面快速定义数据模型,高效,简洁.其实coreData并非完全是对sqlite数据库的封装,也可以使用其他数据库,但我们可以理解为Sqlite.
2)coreData中的核心对象
NSManagedObjectContext: (管理对象上下文 ) 负责应用和数据库之间的交互
NSPersistentStoreCoordinator:(持久化存储协调器)处理数据存储的连接
NSManagedObjectModel: (被管理的对象模型)代表CoreData模型文件,相当于实体
NSEntityDescription:(实体结构)用来描述实体
代码中还常用到的:
NSPredicate:(查询条件)相当于sqlite中的sql语句,但是使用更方便
NSFetchRequest:(获取数据的请求)可以给request设置请求的条件,如:predicate,排序顺序等
2.创建流程
a)创建工程时直接选择使用coreData,会自动创建以 .xcdatamodeld 结尾的coreData数据库,并且在AppDelegate自动生成 上下文:NSManagedObjectContext,对象NSManagedObjectModel,环境NSPersistentStoreCoordinator以及相关的方法(就不一一贴出,创建即可见)
b)工程已存在,创建工程的时候未选择此项,则需要手动添加coreData数据库,并添加 上下文:_managedObjectContext,对象_managedObjectModel,环境_persistentStoreCoordinator以及相关的方法.步骤如下:
1)创建coreData数据库
2)选中数据库,创建对象->修改对象名->添加属性
3)使用创建的对象生成相应地文件:.h和.m
注:在 CourseEntity.m文件中 生成的属性 为@dynamic .不是coreData使用期间,属性不能直接访问(原因如下)
@synthesize :没有自定义存取方法时, 编译器系统自动生成 getter/setter方法
@dynamic :不自动生成getter/setter方法,由自己实现存取方法 或 在运行时动态创建绑定 ,如coreData在实现NSManagedObject子类时使用,由Core Data在运行时动态生成;
补充:我曾经尝试把属性@dynamic -->@synthesize进行转换,让setter/getter 方法自动生成,此时未使用coreData也能直接访问属性,但是问题是:在数据库进行存储时,相关属性的存储会出现问题,猜测setter/getter方法中,coreData中添加了相关的处理......
3.coreData以及相关属性创建完成,进行方法的添加 (代码区)
1)补充未自动创建 AppDelegate中缺失的方法,建议重新创建一个类文件,方便管理.原理以及相关属性作用简介已讲,本模块直接上代码.
.h文件:
@interface CoreDataManager : NSObject
@property (nonatomic, strong, readonly)NSManagedObjectContext
*managedObjectContext;
@property (nonatomic, strong, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
+ (CoreDataManager *)sharedInstance;
- (void)saveContext;
@end
.m文件
@implementation CoreDataManager
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
使用GCD dispatch_once创建单例,保证数据库安全
+ (CoreDataManager *)sharedInstance {
static CoreDataManager * _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[CoreDataManager alloc] init];
});
return _sharedInstance;
}
- (NSURL *)applicationDocumentsDirectory {
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:[self coreDataName] withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSString *)coreDataName {
return @"CourseList";
}
持久化存储协调器
- (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:@"DownloadListDemo1.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;
}
coreData 存储支持
#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();
}
}
}
2)coreData中常用到的方法
coreData 常用到增删查改,使用谓词NSPredicate 设置条件进行操作;其中 ,增,删,改 结束后都要进行保存
#define kManagedObjectContext [CoreDataManager sharedInstance].managedObjectContext
增:
CourseEntity *courseEntity = [NSEntityDescription insertNewObjectForEntityForName:@"CourseEntity" inManagedObjectContext:kManagedObjectContext];
courseEntity.courseId = @1101;
courseEntity.courseName = @"P出未知的你";
[[CoreDataManager sharedInstance] saveContext]; //插入 保存
查:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CourseEntity"
inManagedObjectContext:kManagedObjectContext];
[request setEntity:entity];
//使用谓词NSPredicate 添加查询条件 相当于sqlite中的sql语句
NSPredicate *predict = [NSPredicate predicateWithFormat:@"courseId = %d" ,courseId];
if (predicate) [request setPredicate:predicate];
//设置 获得数组的 排序方式(可根据需要 设置添加)
NSSortDescriptor *byCourseName = [NSSortDescriptor sortDescriptorWithKey:@"courseName"ascending:YES];
NSSortDescriptor *byCourseId = [NSSortDescriptor sortDescriptorWithKey:@"courseId" ascending:YES];
NSArray *sortDescriptors = [NSMutableArray arrayWithObjects:byCourseName,byCourseId, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *objectResults = [kManagedObjectContext
executeFetchRequest:request
error:&error];
删: (取出需要删除的对象->删除->保存)
if (objectResults && objectResults.count > 0 ) {
for (NSManagedObject *object in objectResults) {
[kManagedObjectContext deleteObject:object];
}
[[CoreDataManager sharedInstance] saveContext]; //删除之后 保存
}
改:
CourseEntity *couEntity = [objectResults firstObject];
couEntity.courseName = @"动出自己风格";
[[CoreDataManager sharedInstance] saveContext]; //修改之后 保存
coreData单表的基本操作已做一个简单的介绍但他的功能远非如此,它还支持多表关联操作,具体情节,还待下次分享......(欢迎批评指正~~)