views:

870

answers:

4

Hi,

I wanted to create an unique string with length not more than 15 characters in objective C (or with the help of Iphone SDK 3.0).

I need this for mysql table. I know mysql auto increment will do for primary key but I really need to send the unique key from Iphone Itself.Every record in my table had to have an unique key.

The Unique string should be unique and alphanumeric and max 15 characters

Thanks in Advance, Sridhar,

A: 

This other stackoverflow answer is similar check it out.

NWCoder
+1  A: 

Normally, I would recommend a UUID/GUID (as NWCoder does in his link), but a UUID is 16 bytes of raw data, so it cannot be expressed in 15 bytes of data, far less in 15 alphanumeric characters.

So then the question becomes, how "unique"?

Unique to just the table would be easy, just use an incrementing value and an unsigned int or hex data.

Unique to the world would be more challenging. You could get close by generating a UUID, then hashing it with MD5, then taking as many bits of that as you can and converting it into base (10+26+26 for case sensitive, or 10+26 for case insensitive) and taking the first 15 characters.

Unique to just the user, but (as you describe later) unique in the presence of non-internet connectivity would most easily be done by simply generating a random string of 15 alphanumerics. This would likely be functionally equivalent to the UUID/MD5 method above - essentially the chance of collission would be very very small, and given the user is only going to have a handful of trips, there is essentially no chance of collissions (assuming you seed the random number generator appropriately).

Peter N Lewis
A: 

Thanks for the replys.

Here what exactly I need is :

My app users create "trips",so I have to maintain each trip on my web server using unique ID.So when user creating a trip I need to send the trip unique id along with the trip data.

Why I am oppose to create primary auto increment field in my tables is : If user don't have Internet I am storing the data in plist files. So when he connected to internet It will sync the data to the web server.

Thanks, Sridhar,

why max 15 characters?
David Maymudes
To reduce the table size, if it is more than 15 the size of the data will be increased
Sridhar
+1  A: 

There is a simple answer to this that you are missing. Each trip has a primary key composed of two numbers.

When a trip is created on the iphone it gets a unique number (ie 1,2,3,4,5).

When a trip is synchronised with the server, you give the iphoen a special unique number that goes in front, ie if iphone number is 10421, the trip numbers become 10421.1, 10421.2, etc)

This guarantees that all id's are always unique.

Jacob