Will dateA and dateB always be different?
// Two [NSDate date] following each other
NSDate *dateA = [NSDate date]; // Line X
NSDate *dateB = [NSDate date]; // Line X+1
That is, will the line below always return NO?
[dateA isEqualToDate:dateB]
(“Always” meaning that like a very fast processor wouldn’t execute the two commands so fast that dateA and dateB would be assigned the same time with “sub-second” accuracy).
I want to have a “unique” timestamp for some internal identification (not DB-related).
There is no promise that
dateBwill be afterdateA.NSDateis based on the system clock, which can bump forward or backward based on NTP information. It would be pretty surprising to have twoNSDatetimes collide, but there’s no promise it won’t happen.If you need something a little better, I’d recommend
mach_absolute_time()orCACurrentMediaTime(). They always increase during the run of your program. They’re measure time since the last boot of the device, so they’re only unique until the next reboot. If you need something that always increases, it’s pretty easy to build that by keeping track of an offset.mach_absolute_time()tracks CPU ticks, so I don’t believe two calls to it on the same thread can return the same value.