I am using ionic 2 framework and I have tried using local storage to store a network status
this.local = new Storage(LocalStorage);
this.local.set(\"status\"
This can be achieved with a BehaviorSubject
along with the Storage module from Ionic.
Create a BehaviorSubject that you call .next()
whenever you set a key in Storage and subscribe to that BehaviorSubject to get the updated value.
A code example of this:
let storageObserver = new BehaviorSubject(null)
this.storage.set("key", "data")
storageObserver.next(null)
setTimeout(() => {
this.storage.set("key", "new data")
storageObserver.next(null)
}, 5000)
storageObserver.subscribe(() => {
this.storage.get("key")
.then((val) => console.log("fetched", val))
})
This will set a key in local storage which will be logged to the console, and asynchronously update that key after 5 seconds which then will be logged again to the console. You could refactor the setting of the key and updating the BehaviorSubject to a function.