2017-08-10 100 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