iOS基础-- CoreData 数据库表关联操作

1523603-9e068cad2a77f61e.png
1523603-d5a60d566d9a5a01.png
1523603-1c038933c4eb202c.png
1523603-f950021b3db750a0.png
1523603-86bddf4d56e95419.png




-barButtonItemClicked方法里

//Class

NSEntityDescription*classDescription = [NSEntityDescriptionentityForName:@"LO_Class"inManagedObjectContext:self.objectContext];

LO_Class*myClass = [[LO_Classalloc]initWithEntity:classDescriptioninsertIntoManagedObjectContext:self.objectContext];

intnumber =arc4random() % 100;

myClass.number= [NSNumbernumberWithInteger:number];

//Teacher

NSEntityDescription*teacherDescription = [NSEntityDescriptionentityForName:@"Teacher"inManagedObjectContext:self.objectContext];

Teacher*teacher = [[Teacheralloc]initWithEntity:teacherDescriptioninsertIntoManagedObjectContext:self.objectContext];

teacher.teaName=@"萌萌";

teacher.teaAge= [NSNumbernumberWithInteger:18];

teacher.teaGender=@"男";

//将老师和班级建立连接

myClass.classTeacher= teacher;

teacher.teaClass= myClass;

//Student

inta =arc4random() % 50 + 30;

for(inti = 0; i < a; i++) {

NSEntityDescription*studentDescription = [NSEntityDescriptionentityForName:@"Student"inManagedObjectContext:self.objectContext];

Student*student = [[Studentalloc]initWithEntity:studentDescriptioninsertIntoManagedObjectContext:self.objectContext];

student.stuName= [NSStringstringWithFormat:@"王震威%d号",i];

student.stuAge= [NSNumbernumberWithInteger:18];

NSArray*arr = @[@"男",@"女",@"王德亮"];

intgender =arc4random() % arr.count;

student.stuGender= arr[gender];

//学生和班级,老师建立联系

student.stuClass= myClass;

student.stuTeacher= teacher;

//让班级数据和学生建立一个一对多的关系,多次进行添加

[myClassaddClassStudentObject:student];

//让老师数据和学生建立一个一对多的关系,多次进行添加

[teacheraddTeaStuObject:student];

}

总结

CoreData数据库框架的实现核心是持久化存储栈

与CoreData数据库框架的所有交互都是通过NSManagedObjectContext完成的

NSManagedObjectContext中的数据是缓存在内存中的副本,要想达到持久化目的必须更新保存

代码:

#import"ViewController.h"

#import"Student.h"

#import"Teacher.h"

#import"LO_Class.h"

#import

#import"AppDelegate.h"

@interfaceViewController()

@property(nonatomic,strong)UITableView*tableView;

@property(nonatomic,strong)NSManagedObjectContext*objectContext;

@property(nonatomic,strong)NSMutableArray*dataArray;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.dataArray= [NSMutableArrayarray];

[selfsetTableView];

[selfappdelegate];

[selfsearchAll];

}

- (void)setTableView{

self.tableView= [[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:(UITableViewStylePlain)];

[self.viewaddSubview:self.tableView];

self.tableView.dataSource=self;

self.tableView.delegate=self;

[self.tableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@"cell"];

self.tableView.backgroundColor= [UIColorredColor];

}

-(void)appdelegate{

AppDelegate*app = (AppDelegate*)[UIApplicationsharedApplication].delegate;

self.objectContext= app.managedObjectContext;

self.navigationItem.rightBarButtonItem= [[UIBarButtonItemalloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd)target:selfaction:@selector(rightBarAction)];

}

- (void)rightBarAction{

//Class

NSEntityDescription*classDescription = [NSEntityDescriptionentityForName:@"LO_Class"inManagedObjectContext:self.objectContext];

LO_Class*myClass = [[LO_Classalloc]initWithEntity:classDescriptioninsertIntoManagedObjectContext:self.objectContext];

intnumber =arc4random() % 100;

myClass.number= [NSNumbernumberWithInteger:number];

//Teacher

NSEntityDescription*teacherDescription = [NSEntityDescriptionentityForName:@"Teacher"inManagedObjectContext:self.objectContext];

Teacher*teacher = [[Teacheralloc]initWithEntity:teacherDescriptioninsertIntoManagedObjectContext:self.objectContext];

teacher.teaName=@"萌萌";

teacher.teaAge= [NSNumbernumberWithInteger:18];

teacher.teaGender=@"男";

//将老师和班级建立连接

myClass.classTeacher= teacher;

teacher.teaClass= myClass;

//Student

inta =arc4random() % 50 + 30;

for(inti = 0; i < a; i++) {

NSEntityDescription*studentDescription = [NSEntityDescriptionentityForName:@"Student"inManagedObjectContext:self.objectContext];

Student*student = [[Studentalloc]initWithEntity:studentDescriptioninsertIntoManagedObjectContext:self.objectContext];

student.stuName= [NSStringstringWithFormat:@"王震威%d号",i];

student.stuAge= [NSNumbernumberWithInteger:18];

NSArray*arr = @[@"男",@"女",@"王德亮"];

intgender =arc4random() % arr.count;

student.stuGender= arr[gender];

//学生和班级,老师建立联系

student.stuClass= myClass;

student.stuTeacher= teacher;

//让班级数据和学生建立一个一对多的关系,多次进行添加

[myClassaddClassStudentObject:student];

//让老师数据和学生建立一个一对多的关系,多次进行添加

[teacheraddTeaStuObject:student];

}

//很重要,完成save后才保存到数据库

NSError*error =nil;

[self.objectContextsave:&error];

if(error ==nil) {

NSLog(@"保存正确");

[self.dataArrayaddObject:myClass];

NSIndexPath*indexPath = [NSIndexPathindexPathForRow:self.dataArray.count-1inSection:0];

[self.tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:(UITableViewRowAnimationFade)];

}

}

- (void)searchAll{

// Fetch  ——CoreData 系统封装好的

NSFetchRequest*fetchRequest = [[NSFetchRequestalloc]init];

NSEntityDescription*entity = [NSEntityDescriptionentityForName:@"LO_Class"inManagedObjectContext:self.objectContext];

[fetchRequestsetEntity:entity];

//使用谓词查询,里面类似于SQL查询语句中的where条件

//    LO_Class *myClass = [LO_Class new];

//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"studentClass = %@", myClass];

//    [fetchRequest setPredicate:predicate];

//排序(升降序)

NSSortDescriptor*sortDescriptor = [[NSSortDescriptoralloc]initWithKey:@"number"ascending:YES];

[fetchRequestsetSortDescriptors:[NSArrayarrayWithObjects:sortDescriptor,nil]];

NSError*error =nil;

NSArray*fetchedObjects = [self.objectContextexecuteFetchRequest:fetchRequesterror:&error];

if(error ==nil) {

[self.dataArraysetArray:fetchedObjects];

[self.tableViewreloadData];

}

}

//tableView必须实现方法

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"cell"];

LO_Class*class =self.dataArray[indexPath.row];

Teacher*teacher = class.classTeacher;

cell.textLabel.text= [NSStringstringWithFormat:@"%@班级的%@老师",class.number,teacher.teaName];

returncell;

}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

returnself.dataArray.count;

}

@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值