How to avoid duplicate data entry in a table in Room Db?

Ravi Rajput :

I am using RoomDb to store the messages in the device using room database. Each Message contain a unique Id which is generated when storing message on a server. When a message is downloaded and stored in a Room database and again if I try to download the message it again gets downloaded and saved to room db.

I tried using replace strategy, but still it doesnt works

 @Insert(onConflict = OnConflictStrategy.REPLACE)
    void saveMessage(ArrayList<Message> messages);

The above code is supposed to replace the existing message, but its not doing so.

Message model looks like this.

public class Message {

    @Exclude
    @PrimaryKey(autoGenerate = true)
    public long _id;

    @Exclude
    @ColumnInfo(name = "messageId")
    public String id;

    @Exclude
    public boolean outbox;
    @Exclude
    public boolean pending;

    @Exclude
    public boolean draft;

    @Exclude
    @ColumnInfo(typeAffinity = ColumnInfo.BLOB)
    public byte[] thumbnail;
    @Exclude
    public boolean downloaded;
    @Exclude
    public boolean seen;
    @Exclude
    public boolean liked;
    @Exclude
    public boolean disliked;
    @Exclude
    public String path;     // Local attachment path

    @Exclude
    public String localFilePath; //Local attachment file path

    public String title;
    public String body;
    public String type;
    public String image;
    public String file;
    public String audio;
    public String video;


}
KuLdip PaTel :

you have to change your Entity class like this

In Java

@Entity(tableName = "chat_message_table", indices = @Index(value = {"messageId"}, unique = true))
public class Message {
...
}

In Kotlin

@Entity(tableName = "chat_message_table", indices = [Index(value = ["messageId"], unique = true)])
data class Message(@ColumnInfo(name = "messageId") val messageId: String) {
 @PrimaryKey(autoGenerate = true)
 var _Id: Int = 0
 ...
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to secure table for avoid duplicate data

Laravel avoid duplicate entry in child table

How to avoid duplicate data in roomdatabase?

moving table data error ‘#1062 - Duplicate entry‘

How to avoid duplicate insert in DB through Java?

How to avoid blank entry into signup textfields which stores data in mysql db in Java? How to avoid logging in without filling username and password?

Duplicate class found in file, Data Binding, Room DB

How do I avoid duplicate object entry in Mongodb?

How to update or insert data with WITH subquery? and avoid `PG::UndefinedTable: ERROR: missing FROM-clause entry for table` error

Unable to insert two different data into MYSQL DB - shows duplicate entry

how to restrict entry of duplicate object in core data

MySQL - How to remove duplicate data entry?

How to avoid duplicate record while inserting data using user defined table type in SQL Server

SQL Server : how to avoid duplicate data?

How to Avoid Duplicate Data RestfulApi Laravel

pymongo 2.8 - how to avoid inserting duplicate user in admin db

How to avoid adding duplicate array entry and instead update the already existing entry

How to avoid duplicate entry and get the ID of corresponding originial entry using NHibernate

How to avoid force close if there is no data to fetch in db

Room db with many to many relations : returns single entry on querying for filtered data

How to avoid duplicate rows with junction table. (How to use it)?

Validating inputs in a function in java to avoid duplicate data other than non entry with default of 0 (No database )

Avoid duplicate entry in xml file c#

avoid duplicate entry error in updating unique column

How to avoid to duplicate columns when getting a table from a json element

how to prevent duplicate entry in mysql while data are entered from array?

How to fetch data without a duplicate entry by using limit and order by

Avoid display duplicate elements in table

insert in same table avoid duplicate