Question: I am learning how to use ROOM as part of Android Architecture Components. For a regular recyclerviews, I am able to implement `onSwipe` to delete a post on the list easily and my `ViewModel` will directly calls my repository to call ROOM to delete the given post. 

However, I ran into some issues when I tried to also implement the `onMove` callbacks. the callback listed as below: 

```
public boolean onMove(@NonNull RecyclerView recyclerView,
                      @NonNull RecyclerView.ViewHolder viewHolder,
                      @NonNull RecyclerView.ViewHolder target) 

```

from here i can get the before and after positions, my naive thinking is that I should be able to **swap** these two post in my Repository and my LiveData will update the list automatically. 

But I don't know how to do the operation (**swap**) on `DAO` and `ROOM` parts. Can you please share your idea or correct me if I am heading to the wrong direction?

Answer: 
In your repository you will find something like :

`var allRecords : LiveData<List<Record>> = fragmentDao.getAllRecordsOrderedByIDDesc()`

This ensures that the feed into Recycler display (i.e. LiveData) will be current with any inserts, updates etc, to the database.

In the DAO you'll see something like:

```
@Query ("SELECT * from record_table ORDER BY id DESC" )
fun getAllRecordssOrderedByIDDesc(): LiveData<List<Record>>

```

Since you cannot modify id after drag and drop, you'll need a new database column to record position. Let's call it 'position'. Your LiveData would now be:

```
var allRecords : LiveData<List<Record>> = fragmentDao.getAllRecordsOrderedByPositionDesc()

```

based on:

```
@Query ("SELECT * from record_table ORDER BY position DESC" )
 fun getAllRecordssOrderedByPositionDesc(): LiveData<List<Record>>

```

Once a new position is recorded in the database, it is persistent i.e. you can stop the app, restart it, and the record will still appear in the new position (LiveData is using positions read from the database, as described above).

Another approach is to use preferences to record drag and drop changes. After the database is loaded these are then applied. There are pro and cons. Updating a position in a database might be done with a database update, called after recyclerView.adapter.notifyItemMoved(from, to) (in override fun onMove), in override fun clearView.

You'll need code to determine new positions. It is more complex than 'item at position 3 goes to position 5).For example, dragging item at adapter position 3 to position 10 implies the following changes:

```
3 -> null
4 -> 3
5 -> 4
...

9 -> 8
10 -> 9
and finally 3 -> 10

```

The java.util, Collections.swap can be used:

```
from = viewHolder.adapterPosition
to = target.adapterPosition
Collections.swap(mutableList, from, to)

```

in override fun onMove(...) You can then use the mutableList to drive the database position update.
[For drag and swipe in recyclerview follow this url](https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf) 

However ,If you want to persist the new order of list .

* maintain the position in table like 1,2,3....
* fetch the list using Room query - sort the items using position
* user drag and drop ,update the new positions in database 

	+ to get positions => viewholder.getAdapterPosition() ,target.getAdapterPosition()
* Livedata will be fired when there is change in database ,you will get new list from database.