Provider/server: Provide the data or contain the data?

I don’t quite follow this, because reading from Local Storage is expensive (i.e., slow), so if you’re the only person writing to it (since it’s local) the situation is different from reading and writing to a database that someone else might be writing to also.

If you are the sole writer, it’s most efficient to read once upon app startup, and then only write after that, along the lines of: Let’s suppose you care about one array of string that is your checklist.

  1. In ChecklistProvider: private _checklist: ReplaySubject<Array<string>> = new ReplaySubject<Array<string>>();
  2. In provider: getChecklist(): Observable<string[]> { return this._checklist.asObservable(); }
  3. In provider: updateChecklist(newList: string[]) { this._checklist.next(newList); }
  4. On App startup:
    a) Read once from local storage and call updateChecklist with that value
    b) private _checklistUpdater: Subscription = this.getChecklist.distinctUntilChanged().subscribe(new => // store new in local Storage )

If you set things up that way, your cache is updated automatically in the background of your app. So you can write other pages as though they are only modifying the local memory version of the checklist. Then if the battery fails for some reason, your user can power up again, and start with the most recent version in local Storage. The only “disadvantage” to this approach is that it requires pages to read Checklist as an Observable (through getChecklist) instead of a “normal” variable. But with subscribe and the async pipe you can get into that idiom quickly.

I don’t think there’s a one size fits all answer to any of your questions. But if the goal is to have a checklist and a local cache, the strategy I just laid out is the way I’d code it.

1 Like