-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