Home > Blockchain >  Making sure that an itemview is only saved once (Kotlin)(Android)(Room)
Making sure that an itemview is only saved once (Kotlin)(Android)(Room)

Time:12-14

I am unable to find a way to only save an Article only once, if a user were to save an article multiple times duplicates will appear, I need the saving process to only occur once.

enter image description here

  • As expected 2 rows as the uniqueness is on the title column/field and they differ. enter image description here
  • Here the id column is an alias of the special rowid column and is the PRIMARY KEY. But as can be seen, due to the UNIQUE index on the link columnd/field that only 1 row exists as the 2nd attempt was IGNOREd as it had the the same value for the link column/field. enter image description here
  • both rows, as the uniqueness is on BOTH the title and the link columns/fields combined (NOT just the title NOT just the link)

If you wanted neither the title or the link to ever be duplicated then you could have a UNIQUE index on both e.g.

@Entity(
    /* Unique on either the link or the title */
    indices = [
        Index(
            value = ["link"], unique = true
        ),
        Index(
            value = ["title"], unique = true
        )
    ]
)
data class ArticleTitleUniqueByLinkORTitleColumn(
    @PrimaryKey
    var id: Long?=null, /* will be an alias of the rowid */
    var title: String,
    var link: String,
    var publishDateTime: Long
)
  • Related