-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Document how to set an auto increment id? #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi Randy, There was also some related discussion here: Thanks! On Sun, Oct 12, 2014 at 9:55 AM, Randy notifications@github.com wrote:
|
I would also be interested in an auto id feature. Maybe not even an increment but a UUID. |
i use this on android
id always max+1, but it isn't good solution |
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. |
I have a lot of local models which needs an unique identifier. I'm doing something like this for now:
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). |
@markini Collision of UUID is very little chance. |
@Rexota yeah, we decided to just go without collision detection. |
@bmunkholm any update on it? |
@ronanrodrigo Im afraid that this is bad way, I insert list of 8 and pairs of 3 have same ID (I use getNanos, miliseconds doesnt work for sure) |
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.
|
For now, I use something like this (For inserting List of RealmObjects):
Miliseconds as id fail me because I get same id for 3 objects. My temp method lower performance quite a bit, inserting Lists of 8x RealmObjects (object has: long, String, String, int), 5000 times without uniqueId on average takes 3.755sec, with 5.312sec, but every next insertion (when database increase by 5000x8 objects) it adds up about 6.5sec every time, due to checking max id in the database. EDIT: this can be done better, just save maxId in onCreate and then use it till you are done with Realm, and when you wanna use Realm again just get maxId again, not every time like in code above. |
You don't have to run that query every time, just once when the app start:
I would probably encapsulate it in something like a |
@cmelchior great work around. 👍 |
@cmelchior, I think this is a good workaround as well. The problem lies in usage in an app that uses multiple Model classes. The Application's This is something usually handled below the surface in most databases (I'm thinking MySQL and SQLite). Is this something you guys are trying to target for the official 1.0.0 release? |
@fawaad I think the next release that's due (hopefully) next week should address this. |
I just migrate from Active Android to Realm because I saw a lot of people talking about this lib, as far as I want to test Reactive as well. But the lack of AutoIncrement is very hard. // .... // Create new object But It will be great to have this method done each time we make a new XXXRealmObject . |
Until we can implement support for auto-generating primary keys in the bindings we should document the work-arounds. There is 4 cases:
Something like:
|
I just wanted to share my attempt on solving this Problem, because i don't want to pass a primary Key value all the time. First i created a Database-Class to handle the storage of a RealmObject.
After creating the database-class i created an Interface to distinguish between Objects with a Primary Key and without.
Now you will just need to edit this code of the previous execute method
With this solution the database logic will still be in one class and this class can passed down to every class which needs to write into the database.
Please give me some feedback. I am new to Realm and i thought it would be a good idea to handle it like this because i dont need to handle the primary key anymore because i only need to know the primary key when inserting into the database. |
@kejith I think You should only do |
This is how I'd do it: realm.executeTransaction(new Realm.Transaction() { // must be in transaction for this to work
@Override
public void execute(Realm realm) {
Number currentIdNum = realm.where(User.class).max(UserFields.ID);
int nextId;
if(currentIdNum == null) {
nextId = 1;
} else {
nextId = currentIdNum.intValue() + 1;
}
User user = new User(); // unmanaged
user.setId(nextId);
//...
realm.insertOrUpdate(user); // using insert API
}
} |
@Zhuinden thanks for your advise. I fixed the problem with the null pointer.
|
Ah. Yeah, I've done that before, http://stackoverflow.com/a/31560557/2413303 |
Thanks for your quick answer. I've watched your code (the code u linked) and i think that
Could just be a static class to take a RealmObject and a Realm as parameters for every Method so it isnt needed to implement the Interface on every object? |
Well I had a ___RepositoryImpl class for each class. So it was like
|
@kejith i have noticed that you offered a solution to this problem above you gave us a snippet:
but you highlighted that the problem of null pointer at initial use. |
@beeender does your last action - adding the issue to 3.2 milestone mean we can expect to get the feature pretty soon? |
@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. |
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 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: |
By the way, RealmInteger( #4266) type can be used for id auto increment too. |
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.
Instead of
If item is an object of Item then you can simply get id by using
|
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(){ } harry |
Hey try this: |
@c
that is risky, if you have two id's |
No description provided.
The text was updated successfully, but these errors were encountered: