gpt4 book ai didi

ios - 从一对多关系核心数据的实体中获取对象

转载 作者:行者123 更新时间:2023-11-29 03:24:14 24 4
gpt4 key购买 nike

enter image description here

我一直在绞尽脑汁地想自己解决这个问题,但不幸的是我做不到。

所以我的问题是我有多个值对应于一个讲座,所以我制作了 LectureTpPbl、lectureToProfessor、lecturetoStudyProgram 一对多关系类型的关系,如图所示。

我保存到数据库中,如下所示:

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

所有数据都已正确保存,但我在获取数据时遇到问题。现在假设我必须获取一个学期 ID=7、月=12、日=20、(在关系表中)sMID=2 和(在关系表中)pBLID=4 的讲座。

到目前为止我所做的是获取的是

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

如果有一个 sMID 和一个 pBLID,则提取工作正常,否则我不会在提取结果中获取对象。假设我有一个学期 ID 为 7、月 = 12、日 = 20 的讲座,并且有多个 sMID(例如 1,2,3)和多个 pBLID(例如 =4,5,6)我想获取月=12、日=20、sMID=2、pBLID=4的讲座

谁能告诉我该怎么做?

任何帮助将不胜感激。谢谢

最佳答案

首先从 LectureDetails 实体中筛选出 month=12 day=20 的 Lecturers:

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];

现在 lectureList 会给你结果。

关于ios - 从一对多关系核心数据的实体中获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20676495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com