在前面两小节中已介结了COREDATA自动生成MODEL框架。
本节主要对COREDATA进行基本的数据操作。
首先为了能进行演示,动态创建了几个按钮;
代码如下:
- (void)CreateButton
{
UIButton *newbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[newbtn setTitle:@"insert" forState:UIControlStateNormal];
CGRect rect = CGRectMake(40, 40, 80, 20);
newbtn.tag = 1;
newbtn.frame = rect;
[newbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *modifybtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[modifybtn setTitle:@"edit" forState:UIControlStateNormal];
CGRect rect1 = CGRectMake(180, 40, 80, 20);
modifybtn.tag = 2;
modifybtn.frame = rect1;
[modifybtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *delbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[delbtn setTitle:@"delete" forState:UIControlStateNormal];
CGRect rect2 = CGRectMake(40, 80, 80, 20);
delbtn.tag = 3;
delbtn.frame = rect2;
[delbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *selectbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[selectbtn setTitle:@"query" forState:UIControlStateNormal];
CGRect rect3 = CGRectMake(180, 80, 80, 20);
selectbtn.tag = 4;
selectbtn.frame = rect3;
[selectbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *insert1v1btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[insert1v1btn setTitle:@"insert 1V1" forState:UIControlStateNormal];
CGRect rect4 = CGRectMake(40, 120, 80, 20);
insert1v1btn.tag = 5;
insert1v1btn.frame = rect4;
[insert1v1btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *del1v1btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[del1v1btn setTitle:@"del 1V1" forState:UIControlStateNormal];
CGRect rect5 = CGRectMake(180, 120, 80, 20);
del1v1btn.tag = 6;
del1v1btn.frame = rect5;
[del1v1btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *insert1vNbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[insert1vNbtn setTitle:@"insert 1VN" forState:UIControlStateNormal];
CGRect rect6 = CGRectMake(40, 160, 80, 20);
insert1vNbtn.tag = 7;
insert1vNbtn.frame = rect6;
[insert1vNbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *del1vNbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[del1vNbtn setTitle:@"del 1VN for 1" forState:UIControlStateNormal];
CGRect rect7 = CGRectMake(180, 160, 80, 20);
del1vNbtn.tag = 8;
del1vNbtn.frame = rect7;
[del1vNbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *del1vNforNbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[del1vNforNbtn setTitle:@"del 1VN for N" forState:UIControlStateNormal];
CGRect rect8 = CGRectMake(40, 200, 80, 20);
del1vNforNbtn.tag = 9;
del1vNforNbtn.frame = rect8;
[del1vNforNbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:newbtn];
[self.window addSubview:modifybtn];
[self.window addSubview:delbtn];
[self.window addSubview:selectbtn];
[self.window addSubview:insert1v1btn];
[self.window addSubview:del1v1btn];
[self.window addSubview:insert1vNbtn];
[self.window addSubview:del1vNbtn];
[self.window addSubview:del1vNforNbtn];
}
添加点击事件处理方法:其中的调用方法后面将一一列出。
- (IBAction)btnClick:(id)sender
{
switch (((UIButton*)sender).tag) {
case 1:
[self insertData];
break;
case 2:
[self modifyData];
break;
case 3:
[self deleteData];
break;
case 4:
[self queryData];
break;
case 5:
[self insert1V1Data];
break;
case 6:
[self delete1V1Data:2];
break;
case 7:
[self insert1VNData];
break;
case 8:
[self delete1VNof1Data];
break;
case 9:
[self delete1VNofNData];
break;
default:
break;
}
}
修改下面方法添加
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
//在这里进行动态创建按钮
[self CreateButton];
[self.window makeKeyAndVisible];
return YES;
}
运行界面如下:
下面执行基本的数据增删改,操作。
新增:
//插入表数据
- (void)insertData
{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *carItem = [NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:context];
[carItem setValue:@"engin for b" forKey:@"engine"];
[carItem setValue:@"name for bmw" forKey:@"name"];
[carItem setValue:@"wheel for mq" forKey:@"wheel"];
NSManagedObject *carStyleItem = [NSEntityDescription insertNewObjectForEntityForName:@"CarStyle" inManagedObjectContext:context];
[carStyleItem setValue:@"cl" forKey:@"color"];
//要注意数据类型,这里值和类型不一至时异常错误时提示不友好,不好排查
[carStyleItem setValue:[NSNumber numberWithInt:12] forKey:@"width"];
[carStyleItem setValue:[NSNumber numberWithInt:200] forKey:@"height"];
//设置关系
[carStyleItem setValue:carItem forKey:@"main"];
[carItem setValue:carStyleItem forKey:@"details"];
NSError *saveerror;
if (![context save:&saveerror])
{
NSLog(@"don't save %@",[saveerror localizedDescription]);
}
}
修改数据:
//修改数据 这里只是对上面插入数据的某个记录进行查找出来后进行修改。
- (void)modifyData
{
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:context];
NSFetchRequest *cmd = [[[NSFetchRequest alloc]init]autorelease];
[cmd setEntity:entity];
//设置查询条件
NSPredicate *condition = [NSPredicate predicateWithFormat:@"name = %@",@"name for bmw"];
[cmd setPredicate:condition];
NSArray *records = [context executeFetchRequest:cmd error:nil];
if ([records count]>0) {
NSManagedObject *rec = [records objectAtIndex:0];
//修改数据
[rec setValue:@"modify engine" forKey:@"engine"];
//进行保存
[context save:nil];
}
}
删除数据
//删除数据
- (void)deleteData
{
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CarStyle" inManagedObjectContext:context];
NSFetchRequest *cmd = [[[NSFetchRequest alloc]init]autorelease];
[cmd setEntity:entity];
//设置查询条件
NSPredicate *condition = [NSPredicate predicateWithFormat:@"color = %@",@"cl"];
[cmd setPredicate:condition];
NSArray *records = [context executeFetchRequest:cmd error:nil];
if ([records count]>0) {
NSManagedObject *rec = [records objectAtIndex:0];
[context deleteObject:rec];
[context save:nil];
}
}
查找数据
- (void)queryData
{
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchreq = [[[NSFetchRequest alloc]init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:context];
[fetchreq setEntity:entity];
NSError *err;
NSArray *fetchedObjects = [context executeFetchRequest:fetchreq error:&err];
NSInteger count = [fetchedObjects count];
NSLog(@"record count = %i",count);
for (NSManagedObject *car in fetchedObjects)
{
NSLog(@"name = %@",[car valueForKey:@"name"]);
NSLog(@"engine = %@",[car valueForKey:@"engine"]);
NSLog(@"wheel = %@",[car valueForKey:@"wheel"]);
NSManagedObject *carstyle = [car valueForKey:@"details"];
NSLog(@"color = %@",[carstyle valueForKey:@"color"]);
NSLog(@"width = %@",[carstyle valueForKey:@"width"]);
NSLog(@"height = %@",[carstyle valueForKey:@"height"]);
}
}
本节主要介绍增删改查操作。下节将讲解实体与实体之间的关系删除操作。