Zorgan
Zorgan

Reputation: 9123

Generating a unique ID for an application instance

I'm looking to generate a unique identifier (String) for the application instance.

I need this because I'm storing user profiles in Firestore in this format:

/phones/{phoneId}/users/{user}/

..where there are multiple user profiles on 1 device (no authentication). phoneId is meant to be the unique string identifier for the application instance.

However, when I perform:

var uniqueID = UUID.randomUUID().toString()
Log.d("UniqueID", uniqueID )

uniqueId returns a random string every time. The documentation states that "GUIDs can also be used to uniquely identify an app instance"- but like I said I'm not getting the same string every time uniqueId prints.

Any reason why this is or if there's a better alternative to have an immutable string to uniquely identify the app instance?

Upvotes: 0

Views: 2084

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599166

Every time you call UUID.randomUUID().toString() it generates a unique ID. That is by definition. If you want a single unique ID, you should only call UUID.randomUUID().toString() once and store the value in something like SharedPreferences between app starts.

Note that this is pretty similar to the purpose of Firebase Instance IDs, so you might also consider using that to identify the device.

Upvotes: 1

Related Questions