Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
angular load data in component only when indexedDb table is populated

In my service I have this:

  synchronizeCitiesOnLogin() {
    ....
    this.getCitiesFromApi().subscribe(
      cities => {
        cities.map((city) => {
            this.addCityToIndexedDb(city);
        });
      }
    );
  }
  getCitiesFromApi() {
    ....
    return this.apiClient.get(url, { })
      .pipe(
        map((response: any) => response.data ),
        catchError(errorRes => {
          return throwError(errorRes);
        })
      );
  }
  addCitiesToIndexedDb(city) {
    this.cityTable
      .add(city)
      .then(async () => {
        const allItems: CityModel[] = await this.cityTable.toArray();
      })
      .catch(e => {
        alert('Error: ' + (e.stack || e));
      });
  }
    getData(): Promise<any> {
        return this.getDataFromIndexedDb()
    }
    private async getDataFromIndexedDb() {
       ...
       ... 
       return mydata
    }

In my component:

ngOnInit() {
    this.myService.getData().then(data => {
      this.worldCities= data;
    });
}

Because I have a lot of data that I receive from the API it will take some time to save all of them in the IndexedDB. And when I load the page the "fruits" object will be empty because the data were not saved yet in the IndexedDB ... It's working if I'm adding a settimeout with 2-3 seconds but for sure it should be a better way to solve it Can somebody help me with this ? Thank you

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can consider adding a BehaviorSubject to myService.

private synchronized$ = new BehaviorSubject(false);

Thereafter you need to somehow catch the completion of cities insertion into indexeddb, with something looking propably like

import { from } from 'rxjs';

synchronizeCitiesOnLogin() {
  this.getCitiesFromApi().pipe(
    switchMap((cities) => combineLatest(
      cities.map(city => from(this.cityTable.add(city)))
    )),
    tap(() => {
      this.synchronized$.next(true);
    })
  ).subscribe()
}

And then you can ensure data is synchronized before accessing it by doing:

 getData(): Promise<any> {
    return this.synchronized$.pipe(
      filter(synchronized => synchronized),
      first(),
      switchMap(() => from(this.getDataFromIndexedDb()))
    ).toPromise();
 }

The above could certainly be made more readable, as I would strongly suggest to go full RxJs, to avoid constantly switching between observables and promise api, by using ngx-indexed-db.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!