Joseph Sang
Joseph Sang

Reputation: 444

Android room insert/update result

I've been using room for a while. I'm from a mysql background where you have to check the values of queries and stuff. In room, I find this a bit complicated because so far I can either declare the dao insert query as void or as long returning the rowId If I return a long, I have to write a listener to notify the UI of success/failure My question is, is this necessary? Do I need the return value of inserts/updates/deletes or are these queries guaranteed to succeed?

Upvotes: 1

Views: 822

Answers (1)

MikeT
MikeT

Reputation: 57053

My question is, is this necessary?

This depends and is hopefully better explained (a least a little anyway) below.

Do I need the return value of inserts/updates/deletes or are these queries guaranteed to succeed? There is no guarantee that they will succeed. However, you may be able to assume they have or use CONFLICT handling.

Much could depend upon how the Entities are coded, for example say you had a simple table (Entity) with an id and a name and for simplicity that you have autogenerate = true and you never allow the id to be specified when inserting. Unless the database is massive (beyond storage device capacity) or tweaked. A unique id will always result.

If the name needs to be UNIQUE then you are introducing a facet that makes it more likely that the insert will not succeed. If you had the onConflictStrategy of the insert as IGNORE, then a duplicate wouldn't fail but you'd may want to know if nothing was inserted (-1 returned).

This is just one facet. The answer is really that you need to consider the design of the database and of the app itself. Personally I'd always go with informing the user at least of the abnormal/unexpected which probably then means yes it is necessary (typically it is easier to supress code than add new code).

Upvotes: 1

Related Questions