NSPredicate 返回重复值

Posted

技术标签:

【中文标题】NSPredicate 返回重复值【英文标题】:NSPredicate returns duplicate values 【发布时间】:2014-04-02 09:01:35 【问题描述】:

我在过滤数据时遇到问题。我有两个表,patientconsultation,它们具有一对多的关系,因此 consultation 包含患者 ID。当我将数据存储在 Core Data 中时,我在 UITableView 中获得了患者列表,但是当我在第二个 UITableView 中使用 NSPredicate 按 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 医生申请[2983:a0b] 咨询名称: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>)"

)

【问题讨论】:

显示arrayConsultations的日志。 该患者只有 2 次咨询,显示为 6 次 如果您查看它们不同的 id,您似乎一开始就不止一次地插入了它们 检查您的 coredata 数据库(使用 Liya 之类的工具打开。itunes.apple.com/us/app/liya/id455484422?mt=12 )并确认您没有将重复数据包含在其中 【参考方案1】:

但是您创建数据并将其添加到数据存储中是错误的 - 您确实在数据存储中存在重复数据。

您可以在日志中看到您有一组带有请求的id_patient 的项目,并且在该集合中每个都是不同的对象,有些带有重复的id_consultation

导入数据时,您可能没有检查指定 id_consultation / id_patient 的现有项目。您应该检查和更新现有实例,而不仅仅是创建新实例。

【讨论】:

我该怎么做对不起,但我是 ios 开发中的新手,问题是我从 Web 服务中解析它们我删除旧数据并将其添加到基础当我做这样的事情时它可以但是当我评论解析函数并在离线模式下仅从数据库中读取时,每次我从一个患者传递到另一个患者时它都会重复 记录您删除的咨询。我怀疑有时您实际上并没有删除任何内容(因为consultationsList 是空的)但很难知道... 有没有人帮帮我:) 你直接记录服务器响应数据和处理后的consultationsList(是在服务器上重复还是在处理逻辑中)? 插入没问题,但是后面的重复昏迷:/

以上是关于NSPredicate 返回重复值的主要内容,如果未能解决你的问题,请参考以下文章

创建一个带有换行符的 NSPredicate 作为字符串的一部分

NSFetchedResultsController 中的 NSPredicate 不使用 Category 的 getter

Objective-c NSPredicate - 按不同 NSManagedObjectContext 中的 ObjectId 过滤

如果 NSPredicate 失败,啥输出将返回 Fetch 请求

使用 NSPredicate 获取一对多关系值

使用 NSPredicate 按值过滤 NSDictionary