You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi Randy,
We currently don't support that, but plan to do so.
Until then, you would have to create a field yourself.
Could you please expand on your needs for it?
—
Reply to this email directly or view it on GitHub #469.
jcaiqueoliveira and cianiandreadevsphairo, MihaelIsaev, zijing07, RenanSMoura, petersamokhin and 5 morecianiandreadev, felipe-douradinho, petersamokhin, Lucaslgbr and saptan
When I build a Messaging application I would like to create local fake message(s) with unique IDs and then update them with the actual unique key I get from the server after successful post. The way I do this in SQL is by setting an int id field to Primary Key and then setting String uniqueKey as part of my Unique Key. I've just started experimenting with Realm today, but some of these limitations have really made it difficult for me to migrate to Realm.
jk2K, zhenyuan0502, rogerluan, MihaelIsaev, abou7mied and 1 more
I have a lot of local models which needs an unique identifier.
I pass the id to fragments and threads and fetch them then from realm.
I'm doing something like this for now: model.setId(UUID.randomUUID().toString());
I'm still not sure what I should do when a collision occurs.
Overriding is no option in my case
Letting the app crash could lead to unpredictable states.
I'm thinking about either query every id before adding the model to realm or implement a rollback strategy (not sure how, or if possible).
Has anyone similar use cases?
AtomicLong sounds like a bad idea in a synchronized scenario, also you need to initialize the atomic long on start-up. I've done that and it is a hassle.
None of the strategies in this issue will work in a sync scenario. Only something like UUID.getRandom().toString() would. So this issue is purely about documenting how to generate local keys under the premise that the Realm will never be synced.
As noted in #469 (comment) Asking for auto-incremented ID's is usually the wrong question.
Here is my strategy for auto increment in sync scenario. but did not implement completely yet:
1- Cache RealmObject's ids which is created using UUID.randomUUId in separate Realm Object wether device is online or offline
2- Query cached ids then save the Realmresults in temp list and finally clear cached ids after device get online.
3- Query RealmObjects using their cached ids
4- Assign second id by incrementing 1 to max second id ( In my case second id is barcode number which needs to be autoincremented in sync condition).
In this method I have to wait until cached id's or RealmObjects to get synced completely before quering and deleting them. this is crucial for offline conflict resolution. As a workaround to this
I do the query and deletion after delay to give enough time to realm to get synced. this also will be solved using sync progress feature(requires both download and upload completion callback simultaneously not separately)
The website docs have been updated with a section on how to work with auto-incrementing IDs. This should hopefully provide insight into why they in most cases are not needed, and how to get similar semantics if needed.
If you feel that anything is missing or is unclear in that section, feel free to create a new issue with the details and I'll update it as fast as possible.
@cmelchior Thanks for the tip, i have gone through the documentation though its almost similar to how i was doing it, but still they (Realm) could have published this a long time ago.
In case of ActiveAndroid ORM you do not need to write id column in model, It will automatic generate auto incremented value and you can simply use it.
I am giving a sample model below-
@Table(name="Items")
public class Item extends Model{
@Column(name="name")
public String name;
}
Instead of
@Table(name="Items")
public class Item extends Model{
@Column(name="Id")
public long id;
@Column(name="name")
public String name;
}
If item is an object of Item then you can simply get id by using
item.getId();
So, the correct model is first one. For reference you can click here.
Timestamps could work, but they only have millisecond precision which means that there is a small chance of two timestamps being created at the same time. Using an AtomicInteger would be safer:
e.g.
// Find max value when opening the DB
RealmConfiguration config = new RealmConfiguration.Builder(context).build();
Realm realm = Realm.getInstance(config);
AtomicLong primaryKeyValue = realm.where(Foo.class).max("id").longValue();
realm.close();
// ....
// Create new object
new Foo(primaryKeyValue.incrementAndGet());
that is risky, if you have two id's 9223372036854775807 and -9223372036854775808
it will fail. rare but its possible. its not fool proof. so to speak.
Activity
bmunkholm commentedon Oct 12, 2014
Hi Randy,
We currently don't support that, but plan to do so.
Until then, you would have to create a field yourself.
Could you please expand on your needs for it?
There was also some related discussion here:
https://groups.google.com/forum/#!topic/realm-java/6hFqdyoH67w
Thanks!
On Sun, Oct 12, 2014 at 9:55 AM, Randy notifications@github.com wrote:
mpost commentedon Jan 25, 2015
I would also be interested in an auto id feature. Maybe not even an increment but a UUID.
vuhung3990 commentedon Apr 9, 2015
i use this on android
id always max+1, but it isn't good solution
bobbyflowstate commentedon Jun 9, 2015
When I build a Messaging application I would like to create local fake message(s) with unique IDs and then update them with the actual unique key I get from the server after successful post. The way I do this in SQL is by setting an int id field to Primary Key and then setting String uniqueKey as part of my Unique Key. I've just started experimenting with Realm today, but some of these limitations have really made it difficult for me to migrate to Realm.
markini commentedon Aug 20, 2015
I have a lot of local models which needs an unique identifier.
I pass the id to fragments and threads and fetch them then from realm.
I'm doing something like this for now:
model.setId(UUID.randomUUID().toString());
I'm still not sure what I should do when a collision occurs.
I'm thinking about either query every id before adding the model to realm or implement a rollback strategy (not sure how, or if possible).
Has anyone similar use cases?
44 remaining items
beeender commentedon Mar 29, 2017
@iamtodor we will document it in 3.2 ... probably something like #469 (comment) Realm won't support native auto incremental id in a short term.
Zhuinden commentedon Mar 29, 2017
AtomicLong sounds like a bad idea in a synchronized scenario, also you need to initialize the atomic long on start-up. I've done that and it is a hassle.
cmelchior commentedon Mar 29, 2017
None of the strategies in this issue will work in a sync scenario. Only something like
UUID.getRandom().toString()
would. So this issue is purely about documenting how to generate local keys under the premise that the Realm will never be synced.As noted in #469 (comment) Asking for auto-incremented ID's is usually the wrong question.
monajafi commentedon Apr 10, 2017
Here is my strategy for auto increment in sync scenario. but did not implement completely yet:
1- Cache RealmObject's ids which is created using UUID.randomUUId in separate Realm Object wether device is online or offline
2- Query cached ids then save the Realmresults in temp list and finally clear cached ids after device get online.
3- Query RealmObjects using their cached ids
4- Assign second id by incrementing 1 to max second id ( In my case second id is barcode number which needs to be autoincremented in sync condition).
In this method I have to wait until cached id's or RealmObjects to get synced completely before quering and deleting them. this is crucial for offline conflict resolution. As a workaround to this
I do the query and deletion after delay to give enough time to realm to get synced. this also will be solved using sync progress feature(requires both download and upload completion callback simultaneously not separately)
monajafi commentedon Apr 10, 2017
By the way, RealmInteger( #4266) type can be used for id auto increment too.
cmelchior commentedon Apr 19, 2017
The website docs have been updated with a section on how to work with auto-incrementing IDs. This should hopefully provide insight into why they in most cases are not needed, and how to get similar semantics if needed.
If you feel that anything is missing or is unclear in that section, feel free to create a new issue with the details and I'll update it as fast as possible.
https://realm.io/docs/java/latest/#auto-incrementing-ids
kinsleykajiva commentedon Apr 20, 2017
@cmelchior Thanks for the tip, i have gone through the documentation though its almost similar to how i was doing it, but still they (Realm) could have published this a long time ago.
mrasif commentedon Dec 14, 2017
In case of ActiveAndroid ORM you do not need to write id column in model, It will automatic generate auto incremented value and you can simply use it.
I am giving a sample model below-
Instead of
If item is an object of Item then you can simply get id by using
item.getId();
So, the correct model is first one. For reference you can click here.
adonyas commentedon Feb 19, 2018
hello every one my class looks like this i used it to set random id for book id but i want it to be sequential and increasing plz help
public void Random(){
Random rd=new Random();
txtbook1.setText(""+rd.nextInt(1000+1));
}
harry
kinsleykajiva commentedon Feb 19, 2018
Hey try this:
https://stackoverflow.com/a/39137326/6334851
MkazemAkhgary commentedon Nov 28, 2018
@c
that is risky, if you have two id's
9223372036854775807
and-9223372036854775808
it will fail. rare but its possible. its not fool proof. so to speak.