coreData开发
1.新建数据模型文件*.xcdatamodeld
2.在数据模型文件中创建实体并创建实体间的关系
3.新建NSManagedObject对象,根据实体定义,生成模型对象
4.NSManagedObjectContext 负责应用与数据库间的交互(CRUD)
5.NSPersistentStoreCoordinator 添加持久化数据库(如SQL的数据库),是物理数据存储的物理文件和程序间联系的桥梁,负责管理不同对象上下文
#import "ViewController.h" #import <CoreData/CoreData.h> #import "Person.h" #import "Book.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *bgImageView; @property(nonatomic,retain)NSManagedObjectContext *context; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self openDB]; // [self addPerson]; [self allPersons]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //1.打开数据库 -(void)openDB{ //1.实例化NSManagedObjectModel数据模型,将所有的模型都加载进来(merged合并) NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];//从bundles文件中将所有NSManagedObjectModel合并后,得到最终的数据模型 //2.实例化数据存储的桥梁!!!!!!!!!!!!! NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model]; //3.添加调度,打开或创建数据库 NSError *error = nil; NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [docs[0] stringByAppendingPathComponent:@"my.db"]; NSURL *url = [NSURL fileURLWithPath:path]; [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url//本地db的地址 options:nil error:&error]; if(error){ NSLog(@"打开数据出错:error=%@",error); return; } //4.创建上下文 _context = [[NSManagedObjectContext alloc]init]; _context.persistentStoreCoordinator = store;//添加对应的桥梁 } //2.新增个人记录 -(void)addPerson{ //1.实例化一条个人记录,通过NSEntityDescription实例描述添加一个实例 Person *p = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:_context]; p.name = @"刘德华"; p.age = @(19); p.phoneNum = @"10086"; p.image = UIImageJPEGRepresentation([UIImage imageNamed:@"detail.jpg"], 0.1);//添加NSData数据 //新增书 Book *b = [NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:_context]; b.name = @"奋斗好地方"; b.price = @(0.64); Book *b2 = [NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:_context]; b2.name = @"乔布斯传"; b2.price = @(4.6); NSSet *bookSet = [NSSet setWithObjects:b,b2,nil]; p.books = bookSet;//对多关系 NSError *error = nil; [_context save:&error];//一次保存所有记录 } //3.查询所有用户记录 -(void)allPersons{ //1.实例化一个查询请求 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"]; //2.条件查询 // request.predicate = [NSPredicate predicateWithFormat:@"age < 60 && name LIKE '*华'"]; // request.predicate = [NSPredicate predicateWithFormat:@"%K CONTAINS '华' ",@"name"];//通过KeyPath此处不能用%@,得用%K // request.predicate = [NSPredicate predicateWithFormat:@"ANY books.name CONTAINS '时代'"];//多表查询 NSError *error = nil; NSArray *personArray = [_context executeFetchRequest:request error:&error]; for (Person *p in personArray) { NSLog(@"personName=%@ personAge=%@ personPhone=%@",p.name,p.age,p.phoneNum); for (Book *b in p.books) { NSLog(@"bookName=%@ bookPrice=%@",b.name,b.price); } self.bgImageView.image = [UIImage imageWithData:p.image]; } } @end
结果:
2015-02-20 17:08:01.995 CoredataDemo[2549:90078] personName=张学友 personAge=29 personPhone=10010 2015-02-20 17:08:01.998 CoredataDemo[2549:90078] bookName=剩女时代 bookPrice=0.64 2015-02-20 17:08:01.998 CoredataDemo[2549:90078] bookName=夜空最亮的星 bookPrice=4.6 2015-02-20 17:08:02.001 CoredataDemo[2549:90078] personName=刘德华 personAge=19 personPhone=10086 2015-02-20 17:08:02.001 CoredataDemo[2549:90078] bookName=乔布斯传 bookPrice=4.6 2015-02-20 17:08:02.001 CoredataDemo[2549:90078] bookName=奋斗好地方 bookPrice=0.64