0

This is the inspection of the Revision object, with the LinkedHashMap correctly ordered

After saving it, if I retrieve the same document, I get this.

The 'body' field of the 'retrieved' object appears to be correctly ordered, but if I get it as a Map, the map is disordered.

This is not a big deal to me but I have notice that after replication, the document is saved in the server with the same order that the disordered map I have retrieved on the app.

I really want to have the document correctly ordered on the server for readably purposes, is it possible?

I'm running my code on Android 6.0, even though, may this issue be related to the following link? LinkedHashMap entrySet's order not being preserved in a stream (Android)

Thanks for your help!

Community
  • 1
  • 1
R44
  • 153
  • 1
  • 9

1 Answers1

0

When you use the asMap method on a document object, Sync Android creates a shallow-copy Hashmap (see here). So that's why the Map retrieved from the local revision isn't ordered.

It looks like, deep in the depths, and don't quote me on it as it's a long time since I looked at this code, asMap is used to get the JSON to upload, meaning that unordered Map is used to create the JSON that's uploaded to Couch/Cloudant. (See here, but frankly this is deep in the guts after following quite a code path so probably not super-useful).

I think ordering on Java HashMaps depends on insertion order, which would explain why the order is the same on the server as for the asMap result from the local document object.

Mike Rhodes
  • 1,816
  • 12
  • 15
  • That's what I was afraid of, the asMap method being used during the replication process. I think I'm going to give up the order on server for now, if I find a workaround i'll post it here. Thanks! – R44 Mar 14 '17 at 21:27