ios CoreData架構的使用,對上下文資料的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表串連,ioscoredata

來源:互聯網
上載者:User

ios CoreData架構的使用,對上下文資料的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表串連,ioscoredata

這裡是只是代碼,因為部落格插入圖片效果不是很好,我自己寫的總結比較詳細,有興趣的朋友可以在評論裡留下郵箱,我收到後會發給大家。

轉載註明出處,重視原創者的勞動成果,謝謝!


- (void)viewDidLoad {

    [superviewDidLoad];

    

    [self_creatTable];//插入資料

    

//    [self _query];// 查詢資料


    

    // KVC很霸道,即使readonly通過kvc也可賦值,kvo精華

//    Book * book = [[Book alloc]init];

////    book.name = @"book1";

//    [book setValue:@"book2" forKey:@"name"];

//    NSLog(@"%@",book.name);

}


#pragma mark - _query查詢

- (void)_query{

    // 初始化模型

    NSManagedObjectModel * model = [NSManagedObjectModelmergedModelFromBundles:nil];

    

    //建立基礎庫協調器

    NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinatoralloc]initWithManagedObjectModel:model];

    

    // 擷取沙箱路徑

    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)firstObject];

    path = [path stringByAppendingPathComponent:@"coreData.sqlite"];

   NSLog(@"%@",path);

   NSURL * url = [[NSURLalloc]initFileURLWithPath:path];

    

    //為協調器載入基礎庫路徑和基礎庫類型

    [psc addPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:url options:nilerror:nil];

    // 建立上下文

    NSManagedObjectContext * context = [[NSManagedObjectContextalloc]init];

    context.persistentStoreCoordinator = psc;


    

    // 初始化查詢請求

    NSFetchRequest * request = [[NSFetchRequestalloc]initWithEntityName:@"Teacher"];

    

    //謂詞過濾(查詢條件)

#pragma mark '='

//    NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@",@"zhangks"];

        //    NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = 'zhangks'"];

#pragma mark '>'

    //    NSPredicate * predict = [NSPredicate predicateWithFormat:@"age  >10"];

//        NSPredicate * predict = [NSPredicate predicateWithFormat:@"name  in %@",@[@"zhangsk",@"jack"]];

#pragma mark 'like'模糊查詢

    //    NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@",@"zha*"];

    //    NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@",@"*zha*"];

    //    NSPredicate * predict = [NSPredicate predicateWithFormat:@"name like %@",@"*zha"];

    

    

    

#pragma mark 'and'和'&&'模糊查詢

//    NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@ and age = 19",@"zhangks"];

//    NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = %@ && age = 19",@"zhangks"];

    

#pragma mark 'between'模糊查詢

    // 方式1

//    NSPredicate * predict = [NSPredicate predicateWithFormat:@"age between {20,30}"];

    

    // 方式2

//    NSPredicate * predict = [NSPredicate predicateWithFormat:@"age between %@",@[@"10",@"29"]];

//    predict = [NSPredicate predicateWithFormat:predict.predicateFormat];

    

//    request.predicate = predict;

    

    // 資料排序 ascending=YES順序,ascending=NO倒序

    NSSortDescriptor * sort = [NSSortDescriptorsortDescriptorWithKey:@"name"ascending:YES];

    request.sortDescriptors =@[sort];

    

   NSError * error ;

    // 執行查詢語句

   NSArray * array = [context executeFetchRequest:request error:&error];//查詢結果返回的是數組

//    NSInteger count = [context countForFetchRequest:request error:&error];

    // 查詢結果的個數,返回nsinteger

    

    

//    NSLog(@"%li",count);

    

  /*

//    // KVC方式擷取

//    for (NSManagedObject * student in array) {

//        NSLog(@"————————————————————%@,%li",[student valueForKey:@"name"],[[student/Users/zhangxin/Desktop/OC/UI/5.19/5.21coreData/testtestcoredata/testtestcoredata/ViewController.m valueForKey:@"age"] integerValue]);

//    }

    

//    // 子類KVC方式擷取

//    for (Student * student in array) {

//        NSLog(@"%li,%@",[student.age integerValue],[student valueForKey:@"name"]);

//    }

*///KVC

    

    //通過映射對象擷取

   for (Teacher * teacherin array) {

       NSLog(@"%@",teacher.name);

    }

    


}


#pragma mark _creatTable

- (void)_creatTable{

    // 初始化模型

    NSManagedObjectModel * model = [NSManagedObjectModelmergedModelFromBundles:nil];

    

    //建立基礎庫協調器

    NSPersistentStoreCoordinator * psc = [[NSPersistentStoreCoordinatoralloc]initWithManagedObjectModel:model];

    

    // 擷取沙箱路徑

    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)firstObject];

    path = [path stringByAppendingPathComponent:@"coreData.sqlite"];

   NSLog(@"%@",path);

   NSURL * url = [[NSURLalloc]initFileURLWithPath:path];

    

    //為協調器載入基礎庫路徑和基礎庫類型

    [psc addPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:url options:nilerror:nil];

    // 建立上下文

    NSManagedObjectContext * context = [[NSManagedObjectContextalloc]init];

    context.persistentStoreCoordinator = psc;

    

    

    //    // 插入資料

    //    NSManagedObject * student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];

    //    [student setValue:@"zhangks" forKey:@"name"];

    //    [student setValue:@(19) forKey:@"age"];

   //

    //    NSManagedObject * teacher = [NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:context];

    //    [teacher setValue:@"zhqo" forKey:@"name"];

    

    // 插入資料 建立 NSManagedObject subclass類 ,自動根據model中entity(實體)的表建立出類(繼承自NSManagedObject,擁有NSManagedObject的屬性和方法)所以直接用類名建立即可

//    Student * student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];

//    student.name = @"jack";

//    student.age = @(29);

//    

//    

//    

//    Teacher * teacher = [NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:context];

//    teacher.name = @"limei";


    // 初始化查詢請求

    NSFetchRequest * request = [[NSFetchRequestalloc]initWithEntityName:@"Teacher"];

    

    //謂詞過濾(查詢條件)

//        NSPredicate * predict = [NSPredicate predicateWithFormat:@"name = 'limei'"];

    

//    request.predicate  = predict;

    

 NSArray * array = [context executeFetchRequest:request error:nil];

   for (Teacher * teacherin array) {

        Student * student1 = [NSEntityDescriptioninsertNewObjectForEntityForName:@"Student"inManagedObjectContext:context];

    student1.name =@"jim";

        student1.age =@(12);

        student1.relationship = teacher;


        

        

        

    

    

       NSLog(@"%@,%@",teacher.name,student1.relationship.name);

////        teacher.name = @"liuwu";

////        [context deleteObject:teacher];

//        

    }

    

//   NSSet * deleteSet = [context deletedObjects];// 刪除(未save),執行刪除語句後未儲存前,緩衝中(庫表未修改)儲存的刪除資料

//   NSSet * insertSet = [context insertedObjects];//同上

    

    // 儲存

   NSError * error;

    [contextsave:&error];

}





ios CoreData架構的使用,對上下文資料的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表串連ios CoreData架構的使用,對上下文資料的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表串連ios CoreData架構的使用,對上下文資料的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表串連ios CoreData架構的使用,對上下文資料的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表串連ios CoreData架構的使用,對上下文資料的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表串連ios CoreData架構的使用,對上下文資料的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表串連ios CoreData架構的使用,對上下文資料的增刪改查,表與表之間的關聯,1對多,1對1,謂詞查詢,多表串連

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.