How to store what user have searched and its result

懵懂的女人 提交于 2020-01-16 08:59:42

问题


I want to create a database to store what user have searched, I already have this one :

@Entity(tableName = "user_searched")
data class UserSearched (
    @PrimaryKey
    @NonNull
    @ColumnInfo(name = "item")
    val item: String
)

And then I want to link to it the response, it could be a single item or a list of :

@Entity(tableName = "my_item")
data class MyItem(
    @PrimaryKey
    @NonNull
    @ColumnInfo(name = "id")
    val id: Int,
    @ColumnInfo(name = "name") val name: String,
    @ColumnInfo(name = "description") val description: String
)

But then I don't know how to create the relation, I mean I want to do a query for instance, if the item "android" has searched before, just return the list of MyItem as an answer.

How can achieve it with Room db? I'm using rxJava.

IMPORTANT NOTE

There's a possibility when user search "Android" it contains a list of devices on this case List<MyItem> ok, the thing is if user search for "Google" it can show also items shown with "Android" so, I don't know if a good idea is create like a ForeignKey for MyItem because it can contains more than one...

EDIT

Just created a Join table to link them as follows :

@Entity(
    tableName = "name_item_join", primaryKeys = ["name", "item_id"], foreignKeys = [
        ForeignKey(
            entity = MySearch::class,
            parentColumns = ["name"],
            childColumns = ["join_name_id"]
        ),
        ForeignKey(
            entity = MyItem::class,
            parentColumns = ["item_id"],
            childColumns = ["join_item_id"]
        )
    ]
)
data class NameItemJoin(
    @ColumnInfo(name = "join_name_id")
    val nameId: Int,
    @ColumnInfo(name = "join_item_id")
    val itemId: Int
)

So now, my question is, whenever I add a record into UserSearched and then inside MyItem do I have to manually add them into NameItemJoin? Also do I have to create a @Dao to get all of the items given by name, right?

Flow is : First insert to UserSearched Second insert to MyItem Then I'd like to fill the NameItemJoin so whenever I search for an item_id I get the List or anything.

来源:https://stackoverflow.com/questions/56918019/how-to-store-what-user-have-searched-and-its-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!