Skip to content
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

@Unique: more on conflict options #1023

Open
greenrobot-team opened this issue Nov 15, 2021 · 13 comments
Open

@Unique: more on conflict options #1023

greenrobot-team opened this issue Nov 15, 2021 · 13 comments
Labels
enhancement New feature or request

Comments

@greenrobot-team
Copy link
Member

greenrobot-team commented Nov 15, 2021

@Unique currently supports FAIL and REPLACE that are compatible with Sync.

Proposal to add more options (that likely will not work with Sync):

@Unique(onConflict=IGNORE): ignore the offending object (no changes to the existing conflicting object). If there are multiple unique properties in an entity, this strategy is evaluated first:
if the property conflicts, no other properties will be checked for conflicts.

@Unique(onConflict=UPDATE): the offending object overwrites the existing conflicting object while keeping its ID. Thus, all relations pointing to the existing entity stay intact.
This is useful for a "secondary" ID, e.g. a string "ID". Within an entity, this strategy may be used once only (update target would be ambiguous otherwise).

Multiple strategies in an entity are processed in this order: IGNORE, FAIL, REPLACE, UPDATE.

Originally posted by @greenrobot in #509 (comment)

@RobbWatershed
Copy link

As stated on #509, my own use case can't rely on FAIL not REPLACE, but needs IGNORE.

@fcat97
Copy link

fcat97 commented Nov 23, 2021

Implementing the current issue is enough in most cases, maybe in all cases.
These options are extremely necessary and a must-have for many projects.

But what you think if user can himself decide the priority?

@Unique(onConflict=IGNORE, priority=4)
var name: String = ""

@Unique(onConflict=FAIL, priority=3)
var address: String = ""

@Unique(onConflict=REPLACE, priority=2)
var phone: String = ""

@Unique(onConflict=UPDATE, priority=1)
var lastSeen: Long = 1234567890

The put() operation would be done according to the highest priority.

@chakflying
Copy link

chakflying commented Jan 12, 2022

The UPDATE strategy would be very useful for me. Currently I have to manually implement the update mechanism by running a query() for each object in a list, then set all the fields if an existing is found. The performance is not the end of the world, but I get a lot of log spam and the code looks ugly.

@greenrobot-team
Copy link
Member Author

@chakflying

then set all the fields

This sounds like the existing REPLACE strategy could work for this, no? https://docs.objectbox.io/entity-annotations#unique-constraints

@andrew-ld
Copy link

I would like to move the discussion about my new proposal here.
#1046

@chakflying
Copy link

@greenrobot-team But REPLACE doesn't automatically update the relations? If I have to check whether it got replaced during insert, then fix the relations manually it would be even more messy, I think.

@tanukijs
Copy link

Hello, there is some news? still need that implementation of ConflictStrategy.UPDATE @greenrobot-team

@deliqs
Copy link

deliqs commented May 17, 2022

Has there been any update about ConflictStrategy.UPDATE in the meanwhile? @greenrobot-team

@greenrobot-team
Copy link
Member Author

@deliqs What's your use case? E.g. why can't put be used in your case?

@deliqs
Copy link

deliqs commented May 23, 2022

@greenrobot-team I'm building an offline first mobile app. I link the local/offline data to the central/online data by saving the central PK locally as a separate identifier once the data has been uploaded. I want to keep the local data in sync with the central data. So I currently check if any data is missing or outdated locally and get the missing/updated data from my central database.

Please correct me if I'm wrong, but as far as I can see ConflictStrategy.REPLACE will also replace the existing local PK, and thus breaking all existing relationships. So I have to map the data from my central database on to the corresponding local data. It would be a improvement to the code if I could instead just update the local data by inserting the new data with the matching ConflictStrategy.UPDATE property without breaking the existing relationships.

@greenrobot-team
Copy link
Member Author

greenrobot-team commented May 24, 2022

@deliqs Thanks for the details! Yes, if a replace occurs with ConflictStrategy.REPLACE the new inserted object will have a different @Id. And relations would still point to the removed objects @Id, so would be broken.

@Schmalztopf
Copy link

I'm also looking forward to use this kind of UPDATE strategy. Is there any target date for releasing that feature?

@greenrobot-team
Copy link
Member Author

@Schmalztopf No, we will share once a feature is being worked on and when it's done. To help us track interest, thumbs up the first post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants