How to create unique identifiers for objects on different devices

I am working on an application that will allow the user to edit the same database on multiple devices. Editing will include adding / removing / changing objects, so to facilitate the synchronization task, I would like to assign a unique identifier to objects, and I would like to reduce the likelihood that those IDs that conflict with identifiers generated independently on other devices.

One of my ideas was to use the current time to create a unique identifier. I am currently thinking of creating such identifiers:

unsigned long long idNumber = ([NSDate timeIntervalSinceReferenceDate] * 1000000);

This creates a 64-bit integer, which is the number of microseconds since the countdown. I believe it will take about 500,000 years for this number, so I don’t think I need to worry about problems like Y2K.

I could store NSTimeInterval as an identifier, but it becomes inconvenient for me to compare doubles for equality, although this will probably work. Among other things, I can convert a 64-bit integer to and from a string without losing accuracy, and I don't think I can say the same thing about doubles.

It seems to β€œcrack” me, although I see no reason why this will not work. It also seems like it should be portable for any device that supports "unsigned long long". My only problem is that I need to use 8-byte to store identifiers, although I do not expect the number of IDs on my system to be so large, so I don't think that using memory / disk from identifiers should be a big deal.

Any thoughts / warnings / advice would be greatly appreciated.

Thanks Ron

+4
source share
2 answers

Why don't you create the GUID that caused the CFUUID in iOS? You can read here about the GUID for iPhone.

+13
source

Also, what about using [[UIDevice currentDevice] uniqueIdentifier] ?

+1
source

All Articles