尝试从多个实体中获取记录

Try fetch record from multiple entity

我将尝试使用具有核心数据的多个实体。 两个实体名称中的核心数据是 StudentDetail 两者具有反向关系。关系名称是 Student -> Detail:detail Detail -> Student:student。 尝试从两个实体获取记录并显示在 table 视图中。尝试多个代码但无法获得结果。

第一个密码:

Detail *d;
NSMutableArray *arrObj = [[NSMutableArray alloc]init];
for(Student *tblObj in [d student]){
       [arrObj addObject:tblObj];
}
NSLog(@"Your records related with tableA = %@",arrObj); 

无法加载数组中的数据。

第二个密码:

NSArray* fetchResults = [_mainContext executeFetchRequest:fetchRequest error:nil];
    for (int i; i > 1; i++) {
        Student *tableAObject = fetchResults[i];
        NSString * itemcode = tableAObject.detail.email;
        NSLog(@"%@",itemcode);
    }

无法显示记录。

第三种方式:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *aEntity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:_mainContext];
    [fetchRequest setEntity:aEntity];

    NSString *relationshipKeyPath = @"detail"; // Set this to the name of the relationship on "SP" that points to the "Code" objects;
    NSArray *keyPaths = [NSArray arrayWithObject:relationshipKeyPath];
    [fetchRequest setRelationshipKeyPathsForPrefetching:keyPaths];

    NSMutableArray *arrObj = [[NSMutableArray alloc]init];
    for(Student *spObj in arrObj)
    {
        NSLog(@"description is %@",spObj.name);
        Detail *codObj = spObj.detail;
        NSLog(@"it has value %@",codObj);
        NSLog(@" unique name is %@",codObj.email);
    }

在核心数据中插入数据的代码:

- (IBAction)submitData:(id)sender {

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate manageObjectContext];

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

    student.name = _nameText.text;
    student.number = _noText.text;
    student.study = _studyText.text;

    Detail *detail = (Detail *)[NSEntityDescription insertNewObjectForEntityForName:@"Detail" inManagedObjectContext:context];

    detail.number = _noText.text;
    detail.email = _emailText.text;
    detail.address = _addressText.text;
    detail.contact = _contactText.text;

    student.detail = detail;
    detail.student = student;
    NSError *error = nil;
    [context save:&error];

    if (![context save:&error]) {
        NSLog(@"error in adding data %@, %@", error, [error userInfo]);
        abort();
    }
}

如果您需要更多详细信息,请告诉我。谢谢。

您尝试的前两种方法将不起作用,因为没有与从核心数据中获取数据相关的代码。第三种方式的问题是你没有执行你的获取请求。 以下是从核心数据中获取数据的方法。

NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
NSError *error = nil;
NSArray *students = [context executeFetchRequest:req error:&error];

for (Student *stdObj in students)
{
    //Student object
    NSLog(@"Name %@",stdObj.name);
    NSLog(@"Number %@",stdObj.number);

    //Detail object related to student
    Detail *detailObj = stdObj.detail;
    NSLog(@"Email %@",detailObj.email);
    NSLog(@"Address %@",detailObj.address);
}