iOS中coreData的用法
2013-06-30 22:31 甘超波 阅读(8532) 评论(1) 编辑 收藏 举报// // ViewController.m // coredatademo002 // // Created by ganchaobo on 13-6-29. // Copyright (c) 2013年 ganchaobo. All rights reserved. // #import "ViewController.h" #import <CoreData/CoreData.h> #import "Person.h" @interface ViewController () @property(nonatomic,retain) NSManagedObjectContext *context; @end @implementation ViewController #pragma mark -生命周期方法 - (void)viewDidLoad { [super viewDidLoad]; [self LoadData]; //[self UdataData]; // [self addData]; //[self deleteData]; // [self searchdata]; //面向对象操作 [self addPerson]; [self searchPerson]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)viewDidUnload{ [super viewDidUnload]; self.context=nil; } - (void)dealloc { [_context release]; [super dealloc]; } #pragma mark-加载数据方式 -(void)LoadData{ //Nil表是从主bundles中加对应的模型实体 NSManagedObjectModel *model=[NSManagedObjectModel mergedModelFromBundles:nil]; for (NSEntityDescription *desc in model.entities) { NSLog(@"%@",desc.name); } //通过模型 和数据库持久化 NSPersistentStoreCoordinator *storeCoordinator=[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; //持久化到coredata中 NSString *document= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; document=[document stringByAppendingPathComponent:@"coredata.db"]; NSURL *url=[NSURL fileURLWithPath:document]; NSError *error=nil; [storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]; if(error){ NSLog(@"打开数据库失败"); return; } self.context=[[[NSManagedObjectContext alloc] init] autorelease]; self.context.persistentStoreCoordinator=storeCoordinator; } -(void)addData{ //把实体对象和实体上下文相关联 for (int i=0; i<10; i++) { NSManagedObject *obj=[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context]; NSString *name=[NSString stringWithFormat:@"gcb%i",i]; int age=i; [obj setValue: name forKey:@"name"]; [obj setValue:@(age) forKey:@"age"]; //保存上下文中相关联的对象即可 } [self.context save:nil]; } -(void)searchdata{ NSFetchRequest *fetch=[NSFetchRequest fetchRequestWithEntityName:@"Person"]; //排序 NSSortDescriptor *sort=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO]; fetch.sortDescriptors=@[sort]; //加入查询条件 age>20 // fetch.predicate=[NSPredicate predicateWithFormat:@"age>%i",20]; //加入like *c1" //fetch.predicate=[NSPredicate predicateWithFormat:@"name like %@",@"*cb1*"]; NSArray *arr=[self.context executeFetchRequest:fetch error:nil]; for (NSManagedObject *mode in arr) { NSString *name=[mode valueForKey:@"name"]; int age =[[mode valueForKey:@"age"] intValue]; NSLog(@"%zi--%@",age,name); } } //先查询出要修改的数据 -(void)UdataData{ //要操作那一张表 NSFetchRequest *Fetch=[NSFetchRequest fetchRequestWithEntityName:@"Person"]; //先创建排序描述,在排序 NSSortDescriptor *sort=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO]; Fetch.sortDescriptors=@[sort]; //加入查询条件 Fetch.predicate=[NSPredicate predicateWithFormat:@"age>%i",5]; //把条件加入到上下文进行操作,得到查询集合 NSArray * arr=[self.context executeFetchRequest:Fetch error:nil]; for (NSManagedObject *obj in arr) { //更改实体的数据 [obj setValue:@(50) forKey:@"age"]; } //同步更数据库相关联的数据 [self.context save:nil]; } //删除数据, 从数据库中取出来的对象,叫做NSManaedObject对象 -(void)deleteData{ //要找出上下文中操作的一张表 NSFetchRequest *FectchRequest=[NSFetchRequest fetchRequestWithEntityName:@"Person"]; FectchRequest.predicate=[NSPredicate predicateWithFormat:@"age<%i",50]; NSArray *arr=[self.context executeFetchRequest:FectchRequest error:nil]; for (NSManagedObject *obj in arr) { [self.context deleteObject:obj]; } //同步数据库 [self.context save:nil]; } #pragma mark-面向对象开发 -(void)addPerson{ //把要插入的实体和当前上下文相关联 Person *ps=[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context]; ps.name=@"mj__itcast"; ps.age=@28; //同步数据和上下文相关联的 [self.context save:nil]; } -(void)searchPerson{ NSFetchRequest *FetchRequest=[NSFetchRequest fetchRequestWithEntityName:@"Person"]; NSArray *arr=[self.context executeFetchRequest:FetchRequest error:nil]; for (Person *ps in arr) { NSLog(@"name=%@,age=%@",ps.name,ps.age); } } @end
目前我正在专注NLP,请立刻加微信/QQ号 546611623, 免费送你原创《NLP高级执行师》高清视频