0

Since I need offline functionality in my Ionic 2 app and dealing with lots of data, I heavily rely on Storage.

I used to put my objects in maps like

public hashtags:Map<any,Hashtag> = new Map<any,Hashtag>();
...
this.hashtags.set(hashtag.id,hashtag)

stored it like

this.storage.set("hashtags",this.hashtags).then(()=>{
    console.log("stored hashtags");
})

and retrieve it like

this.storage.get("hashtags").then(data => {
    console.log("retrieved hashtags", data);
    this.hashtags = data;
})

On newer devices (Android > 4.4?), this works fine, but on older ones I get [object object] when retrieving the data. It seems that there, objects are saved as strings and cannot be converted back to maps anymore (the driver was asyncStorage).

I have seen that some people store at least arrays in the same way and it seems to work, but since I have a lot of relational data, I would prefer to stick to maps to avoid looping through the arrays for a particular object.

Is there a reliable way to store maps using Storage(2.0.0)? What could be alternatives, if not?

In case of falling back to arrays, what happens to nested objects or arrays of nested objects? like

users:User[] = [new User(1),new User(2)]
let hashtags = [];
hashtags.push(new Hashtag(id,text, users,...))
this.storage.set("hashtags",hashtags)

Thanks!

Chris
  • 4,238
  • 4
  • 28
  • 49
  • Have you configured the storage to a certain type? indexDb, webSql. Not sure if it would help but maybe explicitly settings the storage configuration would set the older devices instead on falling on default –  Mar 23 '17 at 07:30
  • @gerdi before, I had not configured it, so it was working differently on different devices. Meanwhile I added the sqlite plugin, (but still using ionic storage) and it seems to save the data always as strings now. – Chris Mar 24 '17 at 12:53
  • This helped me. I was facing issue similar to yours. https://stackoverflow.com/questions/28918232/how-do-i-persist-a-es6-map-in-localstorage-or-elsewhere – Shailesh Bhattarai Apr 16 '18 at 08:40

0 Answers0