sqliteCoreData单表创建以

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以及相关的主意(就不一一贴发出,创建即可见)

创建工程选择Use Core Data

coreData数据库

b)工程已经是,创建工程的时段不选择这起,则用手动添加coreData数据库,并加加
上下文:_managedObjectContext,对象_managedObjectModel,环境_persistentStoreCoordinator以及有关的方法.步骤如下:

1)创建coreData数据库

1.创建coreData

2)选中数据库,创建对象->修改对象名->添加属性

2.添加表对象以及有关属性

创立 表对象 查看sytle  可清晰明了查属性与涉及相关 

3)使用创造的靶子生成对应地文件:.h和.m

3.1创建对象

3.2中选需要创建对象所于的coreData数据库

3.3抉择着要创建对象的 对象表文件

3.4创建对象对应的.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单表的基本操作已开一个简短的牵线但他的法力远非如此,它还支持多表关联操作,具体情节,还待下次分享……(欢迎批评指正~~)

网站地图xml地图