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

**URL:** <https://forum.ionicframework.com/t/provider-server-provide-the-data-or-contain-the-data/101131>\
**Category:** ionic-v3\
**Created:** [August 7, 2017, 11:55am UTC](https://forum.ionicframework.com/t/provider-server-provide-the-data-or-contain-the-data/101131 "2017-08-07T11:55:47Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [August 14, 2017, 6:20pm UTC](https://forum.ionicframework.com/t/provider-server-provide-the-data-or-contain-the-data/101131/10 "2017-08-14T18:20:38Z")

</div>

> [@Audrey84](#):
>
> I definitely “need” a provider as I have a “data store” (Ionic Storage) where I want to dynamically read, write, replace data. I still have a model class, but that is used in the provider when creating new things (or when reading them from storage, as they have/get the methods I gave them).

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.

---

_[View the full topic](https://forum.ionicframework.com/t/provider-server-provide-the-data-or-contain-the-data/101131)._
