Gangani Roshan
Gangani Roshan

Reputation: 583

Try fetch record from multiple entity

i will try to work with multiple entity with core data. core data in two entity name is Student and Detail both have inverse relationship. relationship name is Student -> Detail:detail Detail -> Student:student. try to fetch record from both entity and display in table view. try multiple code but can't get result.

first code:

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

can't load data in array.

second code:

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

can't display record.

third way:

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);
    }

Code for insert data in core dta:

- (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();
    }
}

if you need more details let me know. Thank you.

Upvotes: 1

Views: 147

Answers (1)

Ravi
Ravi

Reputation: 2451

The first two ways you have tried won't work as there is no code that related to fetch the data from core data. In the third way problem is you are not executing your fetch request. Following is the way to fetch data from core data.

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);
}

Upvotes: 1

Related Questions