-1

I am trying to determine if I have a duplicate record in CoreData using MagicalRecord. To do that, I am comparing the updated data with the first existing record:

This is the code I am using to do the search:

    //  set up predicate to find single appointment
if(selectedApptKey.length > 0)  {
    NSPredicate *predicate =  ([NSPredicate predicateWithFormat:
                                @"((aApptKey == %@) && (aStartTime == %@) && (aEndTime == %@) && (aServiceTech == %@))",
                                selectedApptKey, tStartDate, tEndDate, tStaff]);
    NSLog(@"\n\nselectedApptKey: %@\ntStartDate: %@\ntEndDate: %@\ntStaff: %@",selectedApptKey, tStartDate, tEndDate, tStaff);

    ai = [AppointmentInfo MR_findFirstWithPredicate:predicate inContext:localContext];

    if((ai.aApptKey == selectedApptKey) &&
       (ai.aStartTime == tStartDate) &&
       (ai.aEndTime == tEndDate) &&
       (ai.aServiceTech == tStaff))  {

        updateFlag = YES;
    }
}

This is the predicate data I am using to search for an existing record:

selectedApptKey: RolfMarsh3911 tStartDate: 2013-12-29 20:15:00 +0000 tEndDate: 2013-12-29 22:15:00 +0000 tStaff: Kellie

This is the resulting record as obtained from the above code:

Printing description of ai:
<NSManagedObject: 0xb705430> (entity: AppointmentInfo; id: 0xb7bca00        <x-coredata://649CD00C-3C88-4447-AA2B-60B792E3B25F/AppointmentInfo/p22> ; data: {
aApptKey = RolfMarsh3911;
aEndTime = "2013-12-29 22:15:00 +0000";
aImage = nil;
aNotes = "";
aPosH = 200;
aPosW = 214;
aPosX = 0;
aPosY = 231;
aServiceTech = Kellie;
aServices = "";
aStartTime = "2013-12-29 20:15:00 +0000";
client = nil;
})

As you can see, the record and search data are exact. Qustion is: why is the if statement failing (the updateFlag is not being set to YES)?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • 4
    "==" and pointers... Classic mistake. You're checking if the pointers are equal, not the value pointed. – Larme Dec 29 '13 at 21:15
  • Can you give me a code sample as to what the *if* should be? I don't see what's wrong... – SpokaneDude Dec 29 '13 at 21:20
  • 2
    Use `isEqualToDate` for NSDate, `isEqualToString` for NSString, etc. We don't know what's the type of your variables, so... – Larme Dec 29 '13 at 21:23
  • Ahhh... yes, I remember using that... can you please redo your comment as an answer? – SpokaneDude Dec 29 '13 at 21:24

1 Answers1

3

You need to use isEqualToString: instead of ==, since you want to compare the actual object values, not the pointers. Assuming these are all strings. If you have some date objects use isEqualToDate:.

== always compares the pointers, which in this case are not equal, since they are pointing to different objects.

Josiah
  • 4,663
  • 2
  • 31
  • 49