问题
I set a temporary primary locally and save the object to realm, and later the server assigns a unique key for the object and then I want to update the primary key for the object.
So is it possible to reassign a primary key for the object? And what happens when we reassign a primary key to the object? Will it save the object with the old primary key?
回答1:
It is possible to change the value of a primary key up to Realm 1.2.0.
It is prohibited to change primary key value since Realm 2.0.x.
回答2:
Yes it is possible to change the primary key of an already stored RealmObject, only if the updated primary key does not clash with any existing primary key, otherwise Realm will throw an exception. To actually do this you have to get the RealmObject by querying it and modify manually.
So in your usecase if you can guarantee that the server updated primary keys won't clash with any of your temporary key, it would work without any problem.
However IMO this is not a good thing to do.
来源:https://stackoverflow.com/questions/38329223/is-it-possible-in-realm-to-change-the-primary-key-later