2017-08-10 65 views
0

我第一次使用coredata關係。我有兩個實體School和Book,這些屬性具有以下屬性。 學校:id,userid,權限 書名:id,名稱,描述,持續時間,小時。 School和Book之間有一對多的關係。我的目標是從Book based School.userid獲取數據使用關係,我可以實現它。以下是我用於保存和讀取的代碼。使用循環來填充coredata的sqlite數據庫

- (IBAction)save:(id)sender { 
    NSManagedObjectContext *context = [self managedObjectContext]; 

    NSManagedObject *School = [NSEntityDescription insertNewObjectForEntityForName:@"School" inManagedObjectContext:context]; 
    [School setValue:self.idTextField.text forKey:@"id"]; 
    [School setValue:self.userIdTextField.text forKey:@"userid"]; 
    [School setValue:self.permissionTextField.text forKey:@"permission"]; 

    NSManagedObject *Book = [NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:context]; 
    [Book setValue:self.Id.text forKey:@"id"]; 
    [Book setValue:self.nameTextField.text forKey:@"name"]; 
    [Book setValue:self.descriptionTextField.text forKey:@"discription"]; 
    [Book setValue:self.hoursTextField.text forKey:@"hours"]; 
    [Book setValue:self.durationTextField.text forKey:@"duration"]; 
    [Book setValue:School forKey:@"School"]; 



    [School mutableSetValueForKey:@"Book"]; 

    NSError *error = nil; 
    // Save the object to persistent store 
    if (![context save:&error]) { 
     NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

現在,我想自動填充coredata,而不需要通過接口來保存使用loops.I能夠循環工作,直到填充學校。但是,我無法使用循環對特定用戶ID填充Book表。請指導如何實現此目的。任何幫助和建議將不勝感激。提前感謝。

+0

請出示您正在使用爲循環的代碼填充IDUSUNP – 3stud1ant3

+0

@ user1000,增加...很好地檢查和幫助。 – Prez

+0

請檢查答案,如果您遇到問題請在評論中提問 – 3stud1ant3

回答

0

請在每個if語句添加for循環,並設置您選擇的值

if(i>0 &&i<=100) 
      { 
       [School setValue:@"Apple" forKey:@"userid"]; 

       for(int j = 0; j < 10; j++) { 
        NSManagedObject *Book= [NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:context]; 
        [Book setValue:[NSString stringWithFormat:@"%i", j + 10] forKey:@"id"]; 
        [Book setValue:[NSString stringWithFormat:@"Name: %i", j] forKey:@"name"]; 
        [Book setValue:[NSString stringWithFormat:@"%i", j] forKey:@"hours"]; 

        [Book setValue:School forKey:@"School"]; 

       } 



      } 
else if(i>100 && i<=200){ 
      [School setValue:@"iPhone" forKey:@"userid"]; 
      for(int j = 10; j < 20; j++) { 
        NSManagedObject *wouni = [NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:context]; 
        [Book setValue:[NSString stringWithFormat:@"%i", j + 10] forKey:@"id"]; 
        [Book setValue:[NSString stringWithFormat:@"Name: %i", j] forKey:@"name"]; 
        [Book setValue:[NSString stringWithFormat:@"%i", j] forKey:@"hours"]; 


        [Book setValue:School forKey:@"School"]; 

       } 




     } 

注:如果這是逆一個從學校許多關係到wouni(學校的目的之一是與wounis的許多對象)相關 那麼你不需要重新添加wounis學校,因爲CoreData將採取逆關係

最後的照顧,我覺得你不需要下面的語句在每個if條件,

if (![context save:&error]) { 
      NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 

    } 

只是在功能

對於謂語部分的結尾寫一次,我想你應該添加謂詞是這樣的:

[r setPredicate:[NSPredicate predicateWithFormat:@"SELF.userid = %@",userid]]; 
+0

@Prez請顯示您所寫的代碼 – 3stud1ant3

+0

添加我現在使用的代碼,請檢查。 – Prez

+0

@Prez請檢查更新的答案,我已經添加了謂詞部分,這是否解決您的問題? – 3stud1ant3