Java – realm: the recording order has changed
I'm trying to develop my Android application with real database
Today, I encountered the following problem: I added a record list to the table, and then tried to delete one of them Change after deleting the remaining order (different from the order before deletion) Please refer to the picture below for details
Before deletion
After deleting item 3
The question is: is that a function or a bug? How do I keep the order of records?
I know I can easily get the correct order because I want to add a new field as createtime or something like that, but I want to find a very simple solution as the configuration of realm
Solution
By default, items in realm are not sorted, so you should treat any query results as unordered collections unless you explicitly sort them
Usually, these items appear in the order you insert, but not guaranteed The root cause of technical reasons is that we compress the data on the disk, so if you delete the item in the middle of the list, the last item will be moved to its location
So the answer is: it works as expected. If you want to sort the results, you should use the sorting method