Pankaj Wadhwa
Pankaj Wadhwa

Reputation: 3063

Fetch objects from an entity of one to many relationship core data

enter image description here

I was scratching my head all the way through to get this problem solved my self but unfortunately i couldn't.

So my problem is i have multiple values corresponding to one lectures so i made lectureTpPbl,lectureToProfessor,lecturetoStudyProgram relation of type one to many relationship as shown in image.

i saved into the DB like below:

   -(void)saveToDatabase:(NSMutableDictionary *)inData{
     LectureDetails *lectureDetail = (LectureDetails *)[NSEntityDescription        insertNewObjectForEntityForName:@"LectureDetails" inManagedObjectContext:context];
    [lectureDetail setAverageRating:[inData valueForKey:kLectureAverageRating]];
    [lectureDetail setCategoryID:[inData valueForKey:kLectureCategoryID]];
    [lectureDetail setCategoryName:[inData valueForKey:kLectureCategoryName]];
    [lectureDetail setLectureID:[inData valueForKey:kLectureID]];
    [lectureDetail setSemesterID:[inData valueForKey:@"SemesterID"]];
    [lectureDetail setStartTime:[inData valueForKey:kLectureStartTime]];
    [lectureDetail setRatingType:[inData valueForKey:kRatingType]];
    [lectureDetail setMonth:[NSNumber numberWithInt:[[[[inData valueForKey:kLectureDate] componentsSeparatedByString:@"-"] objectAtIndex:1] integerValue]]];
    [lectureDetail setDateOnly:[NSNumber numberWithInt:[[[[inData valueForKey:kLectureDate] componentsSeparatedByString:@"-"] objectAtIndex:2] integerValue]]];


    for (id prof in [inData valueForKey:@"Professors"]) {
        ProfessorsInLecture *profData = (ProfessorsInLecture *)[NSEntityDescription insertNewObjectForEntityForName:@"ProfessorsInLecture" inManagedObjectContext:context];
        profData.professorID=[NSNumber numberWithInt:[[prof valueForKey:@"ProfessorID"]integerValue]] ;
        profData.professorFirstName=[prof valueForKey:@"ProfessorFirstName"];
        profData.professorLastName=[prof valueForKey:@"ProfessorLastName"];
        profData.ProfessorUserName=[prof valueForKey:@"ProfessorUserName"];

        [lectureDetail addLectureToProfessorObject:profData];
    }
    for (id prog in [inData valueForKey:@"StudyPrograms"]) {
        StudyProgramInLecture *stdyProgramData = (StudyProgramInLecture *)[NSEntityDescription insertNewObjectForEntityForName:@"StudyProgramInLecture" inManagedObjectContext:context];
        stdyProgramData.sMID=[prog valueForKey:@"SMID"];

        [lectureDetail addLectureToStudyProgramObject:stdyProgramData];
    }
    for (id pblData in [inData valueForKey:@"PBLs"]) {
        PBLinLectures *pbl = (PBLinLectures *)[NSEntityDescription insertNewObjectForEntityForName:@"PBLinLectures" inManagedObjectContext:context];
        [pbl setLecturePBLID:[pblData valueForKey:@"LecturePBLID"]];
        [pbl setPBLID:[pblData valueForKey:@"PBLID"]];
        [lectureDetail addLectureToPBLObject:pbl];
    }

    NSError *error1;
    if (![context save:&error1])
    {
        //NSLog(@"ERROR--%@",error);
        abort();
    }
}

all the data is getting save correctly but i am getting problem in fetching the data. Now suppose i have to fetch a lecture whose semesterID=7 and month=12 and day=20 and (in relation table)sMID=2 and (in relation table)pBLID=4.

what i have done so far is to fetch is

-(NSMutableArray *)fetchDataforMonth:(NSNumber *)month andDate:(NSNumber *)dateOnly
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"LectureDetails" inManagedObjectContext:context];

// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY lectureToPBL.pBLID ==%@", [NSNumber numberWithInt:[[[UserDetail sharedInstance].userDetail valueForKey:@"pBLID"]integerValue]]];

NSPredicate *pred1 =
[NSPredicate predicateWithFormat:@"(dateOnly == %@)",dateOnly];

NSPredicate *pred2 =
[NSPredicate predicateWithFormat:@"(month == %@)",month];

NSPredicate *pred3 =
[NSPredicate predicateWithFormat:@"(semesterID == %@)",[NSNumber numberWithInt:[[[UserDetail sharedInstance].userDetail valueForKey:@"semesterID"]integerValue]]];

 NSPredicate *pred4 = [NSPredicate predicateWithFormat:@"ANY lectureToStudyProgram.sMID ==%@", [NSNumber numberWithInt:[[[UserDetail sharedInstance].userDetail valueForKey:@"programID"]integerValue]]];

NSArray *compPredicatesList = [NSArray arrayWithObjects:pred1,pred2,pred3,predicate,pred4, nil];

NSPredicate *CompPrediWithAnd = [NSCompoundPredicate andPredicateWithSubpredicates:compPredicatesList];

[request setPredicate: CompPrediWithAnd];

// Fetch the records and handle an error
NSError *error;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];


return mutableFetchResults;
}

If there is one sMID and one pBLID then fetching is working fine otherwise i am not getting an object in fetch result. Suppose i have a lecture whose semesterID is 7 and month=12 and day=20 and have multiples sMID such as 1,2,3 and multiples pBLID such as =4,5,6 and i want to fetch a lecture whose month=12 and day=20 and sMID=2 and pBLID=4

Can anybody tell me how to do that?

Any Help will be appreciated. Thanks

Upvotes: 0

Views: 1827

Answers (1)

Mavericks
Mavericks

Reputation: 524

First filter the Lecturers whose month=12 and day=20 from LectureDetails entity:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(month == %@) AND (dateOnly == %@) ",month, dateOnly];

[request setPredicate: pred];

NSError *error;

NSArray *lectureList = [context executeFetchRequest:request error:&error];

 then filter this array with pBLID and sMID.

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY lectureToStudyProgram.sMID ==%@) AND (ANY lectureToStudyPBL.pBLID ==%@)", sMID, pBLID];

 lectureList = [lectureList filteredArrayUsingPredicate:predicate];

Now lectureList will give you the result.

Upvotes: 1

Related Questions