使用coreadata第一步便是coredata的創(chuàng)建.
//
// ViewController.m
// coredata
//
// Created by zhaoguodong on 16/6/24.
// Copyright ? 2016年 zhaoguodong. All rights reserved.
//
#import "ViewController.h"
#import "STUDENT.h"
#import "TEACHER.h"
@interface ViewController ()
//聯(lián)系上下文
@property (nonatomic ,strong)NSManagedObjectContext *context;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化 NSManagedObjectContext
self.context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
//關(guān)聯(lián)數(shù)據(jù)庫
NSManagedObjectModel *model = [[NSManagedObjectModel alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"]];
//通過文件模型去初始化數(shù)據(jù)持久化助理
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
//創(chuàng)建文件路徑
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"model.sqlite"];
NSLog(@"%@",filePath);
//關(guān)聯(lián)路徑
//如果需要支持版本迭代的話那么要在options設(shè)置@{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES}
//如果不想迭代設(shè)置為options nil
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:filePath] options:@{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES} error:nil];
self.context.persistentStoreCoordinator = store;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark ---------coreDate的使用需要注意,所有的增刪改操作完成之后需要保存一下---
#增
- (IBAction)add:(UIButton *)sender {
//創(chuàng)建一個學(xué)生
STUDENT *stu1 = [NSEntityDescription insertNewObjectForEntityForName:@"STUDENT" inManagedObjectContext:self.context];
stu1.name = @"光頭強";
stu1.age = @26;
STUDENT *stu2 = [NSEntityDescription insertNewObjectForEntityForName:@"STUDENT" inManagedObjectContext:self.context];
stu2.name = @"??大";
stu2.age = @6;
STUDENT *stu3 = [NSEntityDescription insertNewObjectForEntityForName:@"STUDENT" inManagedObjectContext:self.context];
stu3.name = @"??二";
stu3.age = @4;
STUDENT *stu4 = [NSEntityDescription insertNewObjectForEntityForName:@"STUDENT" inManagedObjectContext:self.context];
stu4.name = @"圖圖";
stu4.age = @1;
//創(chuàng)建一個老師
TEACHER *teac1 = [NSEntityDescription insertNewObjectForEntityForName:@"TEACHER" inManagedObjectContext:self.context];
teac1.name = @"??大師";
teac1.sex = @"雄性";
//吧學(xué)生給老師
teac1.myStudent =[NSSet setWithObjects:stu1,stu2,stu3,stu4, nil];
[self.context save:nil];
}
#刪
- (IBAction)delete:(id)sender {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"TEACHER"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",@"??大師"];
request.predicate = predicate;
NSArray *array = [self.context executeFetchRequest:request error:nil];
for (STUDENT *stu in array) {
[self.context deleteObject:stu];
[self.context save:nil];
}
}
#更新
- (IBAction)update:(UIButton *)sender {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"STUDENT"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",@"圖圖"];
request.predicate = predicate;
NSArray *array = [self.context executeFetchRequest:request error:nil];
for (STUDENT *stu in array) {
stu.name = @"吉吉";
[self.context save:nil];
}
}
#查找
- (IBAction)search:(UIButton *)sender {
//查詢老師表
NSFetchRequest *request =[NSFetchRequest fetchRequestWithEntityName:@"TEACHER"];
NSArray *array = [self.context executeFetchRequest:request error:nil];
for (TEACHER *teac in array) {
for (STUDENT *stu in teac.myStudent) {
//teac有個屬性 myStudent 里面就是他的所有學(xué)生
NSLog(@"STUDENT:name=%@ age=%@",stu.name,stu.age);
}
NSLog(@"TEACHER:name = %@ sex:%@",teac.name,teac.sex);
}
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者