天天看點

IOS資料持久化之Core Data(四) - 多表查詢

在前面說了Core Data的單表操作,下面來說一下多表操作

首先來說一下表

新聞表(News)

新聞分類表(NewsSort)

新聞評論表(NewsComment)

新聞編輯表(NewsEditor)

職位表(Position)

薪水表(Salary)

表直接的關系描述:

新聞分類與新聞之間的關系是1 V N(一對多的關系)

新聞與新聞評論之間的關系是1 V N(一對多的關系)

新聞編輯與新聞之間的關系是1 V N(一對多的關系)

職位與新聞編輯之間的關系是1 V N(一對多的關系)

職位與薪水之間的關系是 1 V 1(一對一的關系)

下面來建立表,建立完成以後如下圖:

IOS資料持久化之Core Data(四) - 多表查詢
IOS資料持久化之Core Data(四) - 多表查詢
IOS資料持久化之Core Data(四) - 多表查詢
IOS資料持久化之Core Data(四) - 多表查詢
IOS資料持久化之Core Data(四) - 多表查詢
IOS資料持久化之Core Data(四) - 多表查詢

下面建立表與表之間的關系

新聞分類與新聞直接的關系 1 V N

IOS資料持久化之Core Data(四) - 多表查詢

新聞編輯與新聞之間的關系 1 V N

IOS資料持久化之Core Data(四) - 多表查詢

新聞與新聞評論之間的關系 1 V N

IOS資料持久化之Core Data(四) - 多表查詢
IOS資料持久化之Core Data(四) - 多表查詢
IOS資料持久化之Core Data(四) - 多表查詢
IOS資料持久化之Core Data(四) - 多表查詢

介紹完了各個表直接的關系,它們之間的關系圖如下 :

IOS資料持久化之Core Data(四) - 多表查詢

接下來,我們建立類,點選File->New->File,選擇NSManagedObject subClass,點選Next,然後勾選所有的entities,如下圖:

IOS資料持久化之Core Data(四) - 多表查詢

然後我們初始化資料,代碼如下:

-(void)SaveData{
    NSManagedObjectContext *context = [self managedObjectContext];
    
    NewsSort *newssort =[NSEntityDescription insertNewObjectForEntityForName:NEWSSORT inManagedObjectContext:context];
    newssort.newssortid =[NSNumber numberWithInt:1];
    newssort.newssortname = @"國内新聞";
    
    
    NewsEditor *newseditor =[NSEntityDescription insertNewObjectForEntityForName:NEWSEDITOR inManagedObjectContext:context];
    newseditor.editorname = @"Jack";
    newseditor.editortel = @"62359633";
    newseditor.newseditorno = @"1";
    
    NewsEditor *newseditor1 =[NSEntityDescription insertNewObjectForEntityForName:NEWSEDITOR inManagedObjectContext:context];
    newseditor1.editorname = @"robbin";
    newseditor1.editortel = @"68432696";
    newseditor1.newseditorno = @"2";
    
    
    
    NewsComment *newscomment =[NSEntityDescription insertNewObjectForEntityForName:NEWSCOMMENT inManagedObjectContext:context];
    newscomment.commentid =[NSNumber numberWithInt:1];
    newscomment.commentcontent [email protected]"看過了";
    newscomment.commentdate =[NSDate date];
    
    NewsComment *newscomment1 =[NSEntityDescription insertNewObjectForEntityForName:NEWSCOMMENT inManagedObjectContext:context];
    newscomment1.commentid =[NSNumber numberWithInt:2];
    newscomment1.commentcontent [email protected]"寫的不錯";
    newscomment1.commentdate =[NSDate dateWithTimeIntervalSinceNow:-10];
    
    
    
    
    News *news = [NSEntityDescription insertNewObjectForEntityForName:NEWS inManagedObjectContext:context];
    
    news.newsid =[NSNumber numberWithInt:1];
    news.newstitle [email protected]"财經新聞";
    news.newscontent = @"央行降息";
    news.newsviewcount =[NSNumber numberWithInt:1];
    news.news_newseditor =newseditor;
    news.newsdate =[NSDate date];
    news.news_newssort = newssort;
    news.news_newscomment =[NSSet setWithObjects:newscomment,newscomment1, nil];
    
    
    Position *position =[NSEntityDescription insertNewObjectForEntityForName:POSITION inManagedObjectContext:context];
    position.positionno = @"11";
    position.positionname = @"Level 1";
    
    Position *position1 =[NSEntityDescription insertNewObjectForEntityForName:POSITION inManagedObjectContext:context];
    position1.positionno = @"22";
    position1.positionname = @"Level 2";
    
    newseditor.newseditor_position = position;
    newseditor1.newseditor_position = position1;
    
   
    Salary *salary =[NSEntityDescription insertNewObjectForEntityForName:SALARY inManagedObjectContext:context];
    salary.sy_no = @"1";
    salary.sy_level =[NSNumber numberWithInt:1];
    salary.sy_scale =[NSNumber numberWithDouble:2.0];
    salary.salary_position = position;
    
    Salary *salary1 =[NSEntityDescription insertNewObjectForEntityForName:SALARY inManagedObjectContext:context];
    salary1.sy_no = @"2";
    salary1.sy_level =[NSNumber numberWithInt:2];
    salary1.sy_scale =[NSNumber numberWithDouble:4.0];
    salary1.salary_position = position1;
    
    
    NSError *error = nil;
    if([context save:&error]){
        NSLog(@"儲存資料成功");
    }else{
        NSLog(@"儲存資料成功");
    }
    
}           

查詢新聞,代碼如下:

-(void)ViewNews{
    NSManagedObjectContext *context =[self managedObjectContext];
    
    NSEntityDescription *entity =[NSEntityDescription entityForName:NEWS inManagedObjectContext:context];
    
    //NSPredicate *predicate =[NSPredicate predicateWithFormat:@""];
    
    NSFetchRequest *fetchRequest =[[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
    
    NSArray *array =[context executeFetchRequest:fetchRequest error:nil];
    
    for(News *news  in array){
        news.newsviewcount =[NSNumber numberWithInt:[news.newsviewcount intValue]+1];
        if([context save:nil]){
            NSLog(@"查詢新聞");
        }else{
            NSLog(@"查詢新聞失敗");
        }
    } 
}           

添加新聞,代碼如下:

-(void)AddNews{
    NSManagedObjectContext *context =[self managedObjectContext];
    News *news =[NSEntityDescription insertNewObjectForEntityForName:NEWS inManagedObjectContext:context];
    news.newsid =[NSNumber numberWithInt:2];
    news.newstitle [email protected]"對話創業者";
    news.newsdate =[NSDate date];
    news.newsviewcount =[NSNumber numberWithInt:0];
    news.newscontent = @"什麼時候融資以及占多少股份";
    
    NSEntityDescription *newssortentity =[NSEntityDescription entityForName:NEWSSORT inManagedObjectContext:context];
    
    NSFetchRequest *fecthRequest =[[NSFetchRequest alloc] init];
    
    [fecthRequest setEntity:newssortentity];
    
    NSArray *arr = [context executeFetchRequest:fecthRequest error:nil];
    if([arr count]>0){
        news.news_newssort = [arr objectAtIndex:0];
    }
    
    NSEntityDescription *newseditorentity =[NSEntityDescription entityForName:NEWSEDITOR inManagedObjectContext:context];
    NSFetchRequest *editorFetchRequest =[[NSFetchRequest alloc] init];
    [editorFetchRequest setEntity:newseditorentity];
    
    NSArray *editorArr =[context executeFetchRequest:editorFetchRequest error:nil];
    if([editorArr count]>0){
        news.news_newseditor = [editorArr objectAtIndex:0];
    }
    
    if([context save:nil]){
        NSLog(@"添加新聞成功");
    }else{
        NSLog(@"添加新聞失敗");
    }
}           

添加評論,代碼如下:

-(void)AddComment{
    
    NSManagedObjectContext *context =[self managedObjectContext];
    
    NSEntityDescription *entity =[NSEntityDescription entityForName:NEWS inManagedObjectContext:context];
    
    //NSPredicate *predicate =[NSPredicate predicateWithFormat:@""];
    
    NSFetchRequest *fetchRequest =[[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
    
    NSArray *array =[context executeFetchRequest:fetchRequest error:nil];
    
    for(News *news  in array){
       
        NewsComment *newscomment = [NSEntityDescription insertNewObjectForEntityForName:NEWSCOMMENT inManagedObjectContext:context];
        newscomment.commentid =[NSNumber numberWithInt:3];
        newscomment.newscomment_news = news;
        newscomment.commentcontent = @"添加了新的新聞評論";
        newscomment.commentdate =[NSDate date];
        
        if([context save:nil]){
            NSLog(@"添加新聞評論");
        }else{
            NSLog(@"添加新聞評論失敗");
        }
        
    }
}           

示例代碼下載下傳