首页 > 代码库 > CoreData简单使用
CoreData简单使用
(1)使用CoreData的时候要先导入CoreData.framework框架
(2)新建Data Model文件,如Person
(3)在建完Data Model文件后,新建和Data Model相关联的NSManagedObject subclass文件
//
// ViewController.m
// zwj-CoreData简单使用
//
// Created by zwj on 14-10-10.
// Copyright (c) 2014年 zwj. All rights reserved.
//
#import "ViewController.h"
#import <CoreData/CoreData.h>
#import "Person.h"
@interface ViewController (){
NSManagedObjectContext *_context;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// [self addUser:@"xiaoniagua" andAge:@(20)];
// [self searchUsers];
// [self deleteUser];
[self updateUser];
}
/**
* 删除人员
*/
-(void)deleteUser{
[self getObjContext];
NSFetchRequest *fetchReq = [[NSFetchRequest alloc ]initWithEntityName:@"Person"];
fetchReq.predicate = [NSPredicate predicateWithFormat:@"name =%@",@"zhuweijun"];
NSLog(@"%@",fetchReq.predicate);
NSArray *aryUser = [_context executeFetchRequest:fetchReq error:nil];
NSLog(@"%d",aryUser.count);
for (Person *p in aryUser) {
NSLog(@"%@",p.name);
[_context deleteObject:p];
}
[_context save:nil];
}
/**
* 更新用户信息
*/
-(void)updateUser{
NSFetchRequest *fetchReq = [[NSFetchRequest alloc ]initWithEntityName:@"Person"];
fetchReq.predicate = [NSPredicate predicateWithFormat:@"name = %@",@"xiaoniagua"];
[self getObjContext];
NSArray *aryUsers =[_context executeFetchRequest:fetchReq error:nil];
for (Person *p in aryUsers) {
p.age = @(88);
}
// 更新完后要做数据同步
[_context save:nil];
}
/**
* 查询人员
*/
-(void) searchUsers{
[self getObjContext];
NSFetchRequest *fetchReq = [[NSFetchRequest alloc ]initWithEntityName:@"Person"];
// 排序
NSSortDescriptor *sortDes = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
fetchReq.sortDescriptors = @[sortDes];
NSError *searchError = nil;
NSArray *aryUser = [_context executeFetchRequest:fetchReq error:&searchError];
for (int i = 0; i < aryUser.count; i++) {
Person *p = aryUser[i];
NSLog(@"%@-%@",p.name,p.age);
}
}
/**
* 添加人员
*/
-(void)addUser:(NSString *)name andAge:(NSNumber *)age{
[self getObjContext];
Person *p = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:_context];
p.name = name;
p.age = age;
NSError *savePersonError = nil;
[_context save:&savePersonError];
}
/**
* 持久化存储调度器
*/
-(void)getObjContext{
// 初始化图形上下文
NSManagedObjectContext *context = [[NSManagedObjectContext alloc]init];
// 设置持久化存储调度器 NSPersistentStoreCoordinator
NSString *strPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"coredata.sqllite"];
NSLog(@"%@",strPath);
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc ]initWithManagedObjectModel:model];
NSError *error = nil;
[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:strPath] options:nil error:&error];
context.persistentStoreCoordinator = coordinator;
_context = context;
}
@end
CoreData简单使用