1.NSManagedObjectContext的創(chuàng)建
- (NSManagedObjectContext *)creatManagedObjectContext
{
//1、創(chuàng)建模型對象
//獲取模型路徑
//modelName 模型名字(帶xcdatamodeld后綴的名字)
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"modelName" withExtension:@"momd"];
//根據(jù)模型文件創(chuàng)建模型對象
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
//2、創(chuàng)建持久化助理 //利用模型對象創(chuàng)建助理對象
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
//數(shù)據(jù)庫的名稱和路徑
NSString *docStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *sqlPath = [docStr stringByAppendingPathComponent:@"mySqlite.sqlite"]; NSLog(@"path = %@", sqlPath);
NSURL *sqlUrl = [NSURL fileURLWithPath:sqlPath];
//設(shè)置數(shù)據(jù)庫相關(guān)信息
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:sqlUrl options:nil error:nil];
//3蚀之、創(chuàng)建上下文
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
//關(guān)聯(lián)持久化助理
context.persistentStoreCoordinator = store;
return context;
}
2.對Entity(實體)的操作
查詢
#pragma mark - 查詢
- (void)queryData:(id)data
{
NSError *error = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Book"];
NSMutableArray *resultArray = [[_myAppdelegate.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (resultArray == nil) {
NSLog(@"error = %@",error);
}else{
NSLog(@"query successful!");
}
for (Book *book in resultArray) {
NSLog(@"name==%@------price==%@\n",book.name,book.price);
}
}
插入
#pragma mark - 插入
- (void)insertData:(id)data
{
Book *book = [NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:_myAppdelegate.managedObjectContext];
book.name = @"軟件開發(fā)";
book.price = [NSNumber numberWithFloat:10.5];
NSError *error = nil;
BOOL isSaveSuccess = [_myAppdelegate.managedObjectContext save:&error];
if (!isSaveSuccess) {
NSLog(@"Error:%@",error);
}else{
NSLog(@"Save successful!");
}
}
刪除
#pragma mark - 刪除
- (void)deleteData:(id)date
{
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *book = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:_myAppdelegate.managedObjectContext];
request.entity = book;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name==%@",@"我的青春"];
request.predicate = predicate;
NSError *error = nil;
NSMutableArray *fetchResult = [[_myAppdelegate.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (fetchResult == nil) {
NSLog(@"delete error = %@",error);
}else{
NSLog(@"delete successful!");
}
for (Book *book in fetchResult) {
[_myAppdelegate.managedObjectContext deleteObject:book];
}
if (![_myAppdelegate.managedObjectContext save:&error]) {
NSLog(@"Error == %@,%@",error,error.userInfo);
}else{
NSLog(@"delete save successful !");
}
}
更新
#pragma mark - 更新
- (void)updateData:(id)data
{
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *book = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:_myAppdelegate.managedObjectContext];
request.entity = book;
//條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name==%@",@"軟件開發(fā)"];
request.predicate = predicate;
NSError *error = nil;
NSMutableArray *fetchResult = [[_myAppdelegate.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (fetchResult == nil) {
NSLog(@"Error= %@",error);
}else{
NSLog(@"update successful!");
}
for (Book *book in fetchResult) {
book.name = @"我的青春";
}
[_myAppdelegate.managedObjectContext save:&error];
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者