Search code examples
cachingparse-platformionic-frameworkparse-cloud-codelocal-datastore

Parse Local Datastore for Ionic App?


I see that Parse.com has a local datastore for iOS and Android. Any solutions for Ionic (or hybrid) Apps?


Solution

  • Not exactly sure what it is you're looking for, but when I want to code my Ionic apps with offline functionality I use the new HTML5 localStorage variable.

    I save snapshots of my app's data and lists into this each time the app backgrounds or exits, so it still has content for the user when they're offline.

    If you want to store objects into localStorage just use JSON.parse/stringify().
    If you want to make it easy to fetch/set objects just modify the prototype:

    Storage.prototype.setObject = function(key, value) {
      this.setItem(key, JSON.stringify(value));
    }
    
    Storage.prototype.getObject = function(key) {
      var value = this.getItem(key);
      if (value) return JSON.parse(value);
      else return false;
    }
    

    localStorage can only hold strings, and its limit varies from browser to browser.

    Other methods I've used for saving larger blobs/images/PDFs/long lists/files is the Apache cordova-plugin-file plugin. Which gives an Ionic App filesystem access (you can cache your own snapshots there if you need offline access. Just check for a connection and update it continually, if there's no connection direct your app to local content rather than online content).

    Really depends what kind of app you have. I use HTML5's localStorage for a checklist app that I have, similar to iOS's reminders app but with more flexibility for different lists, and I save all the User's lists into localStorage as JSON strings. I have a manual or auto update setting so when it detects a connection it syncs with their account to keep it all up to date.

    Another app I'm working on creates PDFs and syncs them too, for which I use cordova-plugin-file since PDFs are rather large.