Android (distributed application) primary key strategy

后端 未结 5 913
刺人心
刺人心 2020-12-24 08:34

I am going to implement a distributed application with multiple mobile clients and a web based server application. So each client and also the server are allowed to generate

5条回答
  •  难免孤独
    2020-12-24 09:17

    Let me see if I get this straight: you need a 32 bit number that's unique to the device? Ok:

    1. Create the number either randomly or by hashing the current nanotime. That'll get you a fairly unique string as it is.
    2. Then ask the server if that number has already been used. If it has, generate the number again and ask again.

    If you hash the nanotime, it is so practically impossible (not totally impossible, collision resistance isn't collision proof) to get the same number. Given the rest of your string, that would make it totally unique. This method doesn't require interactions with the server until it actually needs to use the server. Say the client isn't connected at first start: generate the number, save it, and when it does connect, before anything else happens, check to see if the device exists. If it does, start from scratch. That way you can get a truly unique device ID.

提交回复
热议问题