6

I ran across a problem where I am not really sure how to solve it. The project I am working on currently has a model which partly consists of backend stored data and data from the local database. So what I am trying to Archive is something like that:

Article : [Bunch of Information] & [boolean Subscribed]

The subscribed field is device bound and should not reflect any data on the backend. My question is if it is possible to implement in Room some kind of createIfNotExit() Method that handles the following cases:

  • Article not present locally: store a copy and set Subscribed to false
  • Article present: update all the Information and Keep the Subscribe-Flag untouched

My idea is to split the model into a separate Subscription-Model holding a reference to the Article. This way I could implement it simply via @Update(OnConfict=Update) etc...

Is there a way to implement a simple @Query method in the DAO that performs what I want?

Sorry if this is a really basic question but I couldn't find any material about best practices handling this case.

Thank you in advance!

maperz
  • 196
  • 3
  • 11
  • Add your Entity description. I need PrimaryKeys for writing answer – DeKaNszn Jul 14 '17 at 09:35
  • Well I just started planning the app and this point is the problem I can't figure out yet >< what I need is nothing to special but a generic example for this case :) – maperz Jul 14 '17 at 10:18

1 Answers1

3

For example, your entity is:

@Entity(tableName = "articles")
public final class Article {
    @PrimaryKey
    public long serverId;
    public String title;
    public String url;

    public boolean isSubscribed;
}

You may write this method in DAO:

@Query("INSERT OR REPLACE INTO articles (serverId, title, url, isSubscribed) VALUES (:id, :title, :url,
    COALESCE((SELECT isSubscribed FROM articles WHERE id = :id), 0));")
void insertOrUpdateArticle(long id, String title, String url);

Another option - write this logic in your repository and use two simple operations: select and update

DeKaNszn
  • 2,720
  • 3
  • 26
  • 26
  • Okay thanks this should be exactly what I was looking for :-) – maperz Jul 14 '17 at 11:06
  • 2
    Okay actually I am not allowd to use the INSERT command with room. – maperz Jul 15 '17 at 10:25
  • @HugoCodes how did you implement insertOrUpdate issue with room? Please share any ideas and codes example. – nAkhmedov Jul 25 '17 at 05:33
  • @nAkhmedov After trying to implement it like DeKaNszn suggested I came across problems regarding the SQL and Room/Kotlin, so I gave up and did it the simple way: First you check if the entry is in your database already. If it is you apply the changes to the fields that should change. Afterwards you Insert the new Data which should replace the old one present. It's not the fastest way I guess but at least it is simple to maintain and fast enough for my kind of application. Please let me know if you find a better solution. :) – maperz Jul 25 '17 at 09:45
  • @HugoCodes Yes the same way i am doing but update is not working.https://stackoverflow.com/questions/45299625/android-room-persistence-library-update-not-working – nAkhmedov Jul 25 '17 at 10:33