Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm: Order of records was changed

I'm trying to develop my Android app with Realm database.

Today I got below problem: I added a list of records to table and then try to deleted one of them. after deleting the order of the rest was changed (it's different with the order before deleting). please see the images below to see detail.

Before deleting

enter image description here

After delete the 3rd item

enter image description here

And the question is: That's is an function or an bug? And how Can I keep the order of record?

I know that I can easy to get the correct order as I want with add a new field as createTime or something like that but I want to find an very simple solution as config something for Realm.

like image 756
ThaiPD Avatar asked May 27 '16 10:05

ThaiPD


1 Answers

Items in a Realm are not sorted by default, so you should think of any query result as an unordered set unless you explicitly sorted it.

Generally the items will come out in the order you inserted them in, but it is not a guarantee. The underlying reason technical reason is that we compact the data on the disk, so if you delete items in the middle of a list, the last item will be moved to its place.

So the answer is: It is working as intended, and you should use a sorting method if you want your results to be sorted.

like image 69
Christian Melchior Avatar answered Nov 04 '22 08:11

Christian Melchior