Is possible to make an SQLite request async in Redux? The implementation from the documentation isn't async because dispatch(onLoading(false)) is fired before all DB inserts.
Action
const saveData = async () => {
await dispatch(onLoading(true));
await dispatch(saveSQL('DROP TABLE "bepr_problematiky"'));
await dispatch(
saveSQL(
'CREATE TABLE "bepr_problematiky" ( "id" integer NOT NULL, "uid" integer DEFAULT NULL, "title" varchar(255) NOT NULL, "text" text NOT NULL, "file" varchar(255) NOT NULL );'
)
);
for (const insert of inserts) {
await dispatch(
saveProblematiky(
`INSERT INTO 'bepr_problematiky' VALUES(${insert.id}, ${insert.uid}, '${insert.title}', '${insert.text}', '${insert.file}');`
)
);
}
return await dispatch(onLoading(false));
};
Component
export const saveSQL = (sql: string) => {
return async (dispatch: Dispatch<ProblematikyInterface>) => {
await db.transaction(async (tx) => {
await tx.executeSql(sql, [], (tx, results) => {
dispatch({
type: 'LOAD_PROBLEMATIKY',
payload: []
});
console.log('Saved');
});
});
};
};