Search code examples
androidrealmrealm-mobile-platform

Any way to differentiate Realm Object Server clients?


I have a need to tag objects to relate them to individual unique clients. Is there any property in the Java API that I can use to uniquely identify the client?

  • IP address is inappropriate; there might be multiple clients on different private networks that happen to be assigned the same address.
  • SyncUser doesn't work; a given user can log on from multiple simultaneous clients.
  • SyncSession intuitively feels right, but doesn't have any identifier property.
  • ObjectServerSession.nativeSessionPointer is probably what I'm looking for, but is undocumented, and in any case is inaccessible.

EDIT: Upon reflection, the same need could be met with SyncUser's hashCode() or toJson(), iff I could guarantee each user is only logged in once.


Solution

  • After posting a feature request here, the kind folks at Realm suggested that I use a UUID instead.

    This link on identifiers was particularly good reading.

    Anyhow, it looks like the best answer at present is to follow the Android instance-ID implementation instructions.

    The code to get an IID ultimately looked like this:

    db.iid = InstanceID.getInstance(this).getId();
    

    It's worth noting that there's no need to include all of Google Play Services just to get that function. The following dependency does just fine, and doesn't blow through the 64k DEX limit.

    compile 'com.google.android.gms:play-services-iid:10.0.1'