All works fine, but need to implement a re-order of the list, this also works fine except the updating of the list to search in (page). Now the list only gets populated by the constructor, so it does not get updated (view) until you re-open the application. The update in the database (indexeddb) goes fine with the re-ordering. It is only the update of the page/view I am struggeling with.
How can I solve this ? So that after I re-ordered the list the view gets updated either directly or after re-visiting the page without closing the application.
Any hints ? Thanks.
Service code below :
import { Injectable } from '@angular/core';
import { openDB } from 'idb';
@Injectable({
providedIn: 'root'
})
export class ActivitiesService {
public activities: any = [];
constructor() {
this.fillDB();
}
async fillDB() {
const db = await openDB(dbname, dbversion);
this.activities = await db.getAll('activitycodes');
}
filterItems(searchTerm) {
if (this.activities != null) {
return this.activities.filter((activity) => {
if (activity.description != null && activity.code != null) {
return activity.description.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1 || activity.code.indexOf(searchTerm) > -1;
}
return;
});
}
}
}
I think this is an instance of Segal’s Law, in which somebody with two watches never knows what time it is. Having a single source of truth is something I always strive for, and one way you could go about dealing with this is outlined in this post: after reorder, poke the new array back to the service instead of just storing it locally. I would also do all the storage stuff in the service, instead of the page. If pages only concern themselves with presentation, delegating all data wrangling to services, maintenance becomes a lot easier because you can change data handling strategies by editing only one class.
Incidentally, there is a lot of any in here, that both makes the code hard to read (for an outsider, at least), and hides a lot of potential bugs. There’s also, I believe, a memory leak caused by failure to clean up the subscription made in ionViewDidEnter.