我在过滤数据时出现问题。我有两个表patient
和consultation
,它们具有一对多关系,因此consultation
包含患者ID。当我将数据存储在核心数据中时,我会在UITableView
中获取患者列表,但是当我在第二个NSPredicate
中使用UITableView
过滤每个患者的ID咨询时确定重复值。
- (NSMutableArray *)getAllConsultations
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Consultation" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSString *Id = idPatientConsultation;
int idPatient = [Id intValue];
NSLog(@" id Patient Consultation : %@",Id);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id_patient == %i",idPatient];
[fetchRequest setPredicate:predicate];
NSError *error;
NSArray *arrayConsultations = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(@"Nombre de Consultations : %d",arrayConsultations.count);
NSMutableArray *mutConsultationsList = [NSMutableArray arrayWithArray:arrayConsultations];
return mutConsultationsList;
}
阵列咨询日志:
"<Consultation: 0x8d91680> (entity: Consultation; id: 0x8d7a840 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p117> ; data: {\n \"date_consultation\" = \"2014-03-25 13:28:11 +0000\";\n \"id_consultation\" = 24;\n \"id_patient\" = 2;\n motif = aaaaaaaa;\n observation = rien;\n patient = nil;\n poids = \"55 kg\";\n pouls = 60;\n taille = \"1,80 m\";\n temperature = \"37\\U00b0C\";\n \"tension_arterielle\" = \"12,52\";\n})",
"<Consultation: 0x8d5d840> (entity: Consultation; id: 0x8d88350 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p118> ; data: {\n \"date_consultation\" = \"2014-02-27 11:09:03 +0000\";\n \"id_consultation\" = 21;\n \"id_patient\" = 2;\n motif = \"tout va bien\";\n observation = sava;\n patient = nil;\n poids = 60;\n pouls = 80;\n taille = \"1.65\";\n temperature = \"37\\U00b0C\";\n \"tension_arterielle\" = \"12.42\";\n})",
"<Consultation: 0x8d5fa50> (entity: Consultation; id: 0x8d7de80 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p121> ; data: {\n \"date_consultation\" = \"2014-03-25 13:28:11 +0000\";\n \"id_consultation\" = 24;\n \"id_patient\" = 2;\n motif = aaaaaaaa;\n observation = rien;\n patient = nil;\n poids = \"55 kg\";\n pouls = 60;\n taille = \"1,80 m\";\n temperature = \"37\\U00b0C\";\n \"tension_arterielle\" = \"12,52\";\n})",
"<Consultation: 0x8d5fab0> (entity: Consultation; id: 0x8d80030 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p124> ; data: {\n \"date_consultation\" = \"2014-02-27 11:09:03 +0000\";\n \"id_consultation\" = 21;\n \"id_patient\" = 2;\n motif = \"tout va bien\";\n observation = sava;\n patient = nil;\n poids = 60;\n pouls = 80;\n taille = \"1.65\";\n temperature = \"37\\U00b0C\";\n \"tension_arterielle\" = \"12.42\";\n})",
"<Consultation: 0x8d647e0> (entity: Consultation; id: 0x8d7baa0 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p127> ; data: {\n \"date_consultation\" = \"2014-02-27 11:09:03 +0000\";\n \"id_consultation\" = 21;\n \"id_patient\" = 2;\n motif = \"tout va bien\";\n observation = sava;\n patient = nil;\n poids = 60;\n pouls = 80;\n taille = \"1.65\";\n temperature = \"37\\U00b0C\";\n \"tension_arterielle\" = \"12.42\";\n})",
"<Consultation: 0x8d655a0> (entity: Consultation; id: 0x8d70300 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p129> ; data: {\n \"date_consultation\" = \"2014-03-25 13:28:11 +0000\";\n \"id_consultation\" = 24;\n \"id_patient\" = 2;\n motif = aaaaaaaa;\n observation = rien;\n patient = nil;\n poids = \"55 kg\";\n pouls = 60;\n taille = \"1,80 m\";\n temperature = \"37\\U00b0C\";\n \"tension_arterielle\" = \"12,52\";\n})"
) 2014-04-02 10:15:58.555 doctorApplication [2983:a0b] Nombre des consultation:6
这是在数据库中存储数据的代码
-(void)listeDesConsultations{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://127.0.0.1:8888/services/consultation.php" parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON out : %@",responseObject);
ConsultationsTab = (NSArray *) [responseObject valueForKeyPath:@"items"];
NSLog(@"nombre des elements : %d", ConsultationsTab.count );
for (Consultation *consultation in consultationsList) {
[consultation.managedObjectContext deleteObject:consultation];
}
[self.managedObjectContext save:nil];
for (int i=0; i<ConsultationsTab.count; i++) {
Consultation *consultation = [NSEntityDescription insertNewObjectForEntityForName:@"Consultation"
inManagedObjectContext:self.managedObjectContext];
NSDictionary *consultationDic = [ConsultationsTab objectAtIndex:i];
// NSLog(@"your name is : %@ %@", [consultationDic objectForKey:@"id_consultation"], [consultationDic objectForKey:@"id_patient"]);
//id consultation//
NSString *idConsultation = [NSString stringWithFormat:@"%@",[consultationDic objectForKey:@"id_consultation"]];
int idValue = [idConsultation intValue];
NSNumber *idNumber = [NSNumber numberWithInt:idValue];
consultation.id_consultation = idNumber;
NSString *idPatient = [NSString stringWithFormat:@"%@",[consultationDic objectForKey:@"id_patient"]];
int idVal = [idPatient intValue];
NSNumber *idNum = [NSNumber numberWithInt:idVal];
consultation.id_patient = idNum;
NSString* strDate = [consultationDic objectForKey:@"date_consultation"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:strDate];
// NSLog(@"Date de consultation : %@",date);
consultation.date_consultation = date;
consultation.taille = [consultationDic objectForKey:@"taille"];
consultation.poids = [consultationDic objectForKey:@"poids"];
consultation.pouls = [consultationDic objectForKey:@"pouls"];
consultation.motif = [consultationDic objectForKey:@"motif_consultation"];
consultation.observation = [consultationDic objectForKey:@"observation"];
consultation.temperature = [consultationDic objectForKey:@"temperature"];
consultation.tension_arterielle = [consultationDic objectForKey:@"tension_arterielle"];
}
NSError *error;
if (![self.managedObjectContext save:&error])
{
NSLog(@"Problème d'enregistrement : %@",[error localizedDescription ] );
}
consultationsList = [self getAllConsultations];
[self.tableConsultation reloadData];
NSLog(@"hello");
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error : %@",error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"error"
message:[NSString stringWithFormat:@" %@",error]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}];
}
删除前:
Consultation list (
"<Consultation: 0xa1456e0> (entity: Consultation; id: 0xa137b80 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p197> ; data: {\n \"date_consultation\" = \"2014-03-25 13:28:11 +0000\";\n \"id_consultation\" = 24;\n \"id_patient\" = 2;\n motif = aaaaaaaa;\n observation = rien;\n patient = nil;\n poids = \"55 kg\";\n pouls = 60;\n taille = \"1,80 m\";\n temperature = \"37\\U00b0C\";\n \"tension_arterielle\" = \"12,52\";\n})",
"<Consultation: 0xa1459a0> (entity: Consultation; id: 0xa13d5d0 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p198> ; data: {\n \"date_consultation\" = \"2014-02-27 11:09:03 +0000\";\n \"id_consultation\" = 21;\n \"id_patient\" = 2;\n motif = \"tout va bien\";\n observation = sava;\n patient = nil;\n poids = 60;\n pouls = 80;\n taille = \"1.65\";\n temperature = \"37\\U00b0C\";\n \"tension_arterielle\" = \"12.42\";\n})",
"<Consultation: 0xa145a70> (entity: Consultation; id: 0xa138eb0 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p202> ; data: {\n \"date_consultation\" = \"2014-02-27 11:09:03 +0000\";\n \"id_consultation\" = 21;\n \"id_patient\" = 2;\n motif = \"tout va bien\";\n observation = sava;\n patient = nil;\n poids = 60;\n pouls = 80;\n taille = \"1.65\";\n temperature = \"37\\U00b0C\";\n \"tension_arterielle\" = \"12.42\";\n})",
"<Consultation: 0xa145b40> (entity: Consultation; id: 0xa127be0 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p203> ; data: {\n \"date_consultation\" = \"2014-03-25 13:28:11 +0000\";\n \"id_consultation\" = 24;\n \"id_patient\" = 2;\n motif = aaaaaaaa;\n observation = rien;\n patient = nil;\n poids = \"55 kg\";\n pouls = 60;\n taille = \"1,80 m\";\n temperature = \"37\\U00b0C\";\n \"tension_arterielle\" = \"12,52\";\n})"
)
删除后:
2014-04-02 11:55:24.406 doctorApplication[6760:a0b] Consultation list (
"<Consultation: 0xa1456e0> (entity: Consultation; id: 0xa137b80 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p197> ; data: <fault>)",
"<Consultation: 0xa1459a0> (entity: Consultation; id: 0xa13d5d0 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p198> ; data: <fault>)",
"<Consultation: 0xa145a70> (entity: Consultation; id: 0xa138eb0 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p202> ; data: <fault>)",
"<Consultation: 0xa145b40> (entity: Consultation; id: 0xa127be0 <x-coredata://AF78BE86-9E6A-487C-9E49-F720ACF831E4/Consultation/p203> ; data: <fault>)"
)
答案 0 :(得分:1)
但是,您创建数据并将其添加到数据存储中是错误的 - 您确实在数据存储中存在重复数据。
您可以在日志中看到您拥有一组包含所请求id_patient
的项目,并且在该集合中,每个项目都是不同的对象,其中一些具有重复的id_consultation
。
导入数据时,可能没有检查指定id_consultation
/ id_patient
的现有项目。您应该检查并更新现有实例,而不是仅创建新实例。