Hi there,
Recently I am working on Ionic application which would be hopefully deployed to mobile platforms. Basically Im storing some data in Ionic storage like this:
this.storage.set(‘nickname’,“username”)
.then(() => {this.storage.set(‘level’,1)})
.then(() => {this.storage.set(‘xp’, 0)})
.then(() => {this.storage.set(‘dex’, 1)})
.then(() => {this.storage.set(‘speed’, 1)})
.then(() => {this.storage.set(‘end’, 1)})
.then(() => {this.storage.set(‘str’, 1)})
what I believe is standard approach, just keys and values.
Later when I’ve got the storage setted up like this, I would like to get data out of storage… and here comes the strange thing atleast for me. When I try to get all data out of storage using forEach method of the storage itself and log them into console, the data seems to be sorted alphabetical by keys…
To be clear:
this.storage.forEach((value, key, index) =>
{
console.log("Index: ", index);
console.log("Key: ", key);
})
This code results in log:
Index: 1
Key: dex
Index: 2
Key: end
Index: 3
Key: level
Index: 4
Key: nickname
Index: 5
Key: quest
Index: 6
Key: speed
Index: 7
Key: str
Index: 8
Key: xp
Index: 9
I would expect Key ‘nickname’ to be indexed as 1, because it was inserted first according to the code on top of this post. I havent found any note about storage sorting items, maybe Im missing something, i dont know.
In the end, my question is… why this “sort” happens and is reliable that the data would be always sort like this if I’ll be iterating over them like this and picking up certain indexes every time on every device (for example, if I would like to get data from indexes 1-5, can I be sure, that I will, using this approach, find there keys: dex, end, level, nickname, quest?
Please excuse me, if I’m missing something important.
Thanks in advance.