Room user configurable order by queries

前端 未结 4 648
误落风尘
误落风尘 2021-01-11 09:00

I am migrating an app to use Room from normal Sqlite and one part that I am having trouble with is that there are several queries that have an orde

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-11 09:48

    I'm facing the same problem right now. The thing that can be done is to use CASE:

    SELECT * FROM Table
    ORDER BY 
    CASE WHEN :parameter = 1 THEN Column END ASC,
    CASE WHEN :parameter = 2 THEN Column2 END DESC
    

    But I'm assuming you are using a ViewModel as well, so you will probably have to reinitialise it every time. I think this way is a little bit better than writing 15 queries, maybe not as compact though.

    This answer also has a nice example, but the logic is reversed I believe.

提交回复
热议问题