0

I have a application using ionic + react with a page that a user can upload a image from camera or gallery in that page and i save the imagem as binary on the database (indexed db using dexie) so far so good everything works as expected But when the user press backbutton after the image selection and the database save code is not completed happens that the save never ocurs...

Question 1: is it normal behavior? that a code that is running just hang execution when pressing backbutton?

Question 2: there is something i can do to make a code execute no matter what and solve my problem?

some code to be added:

the button just calls a commit function that call this function internally:


const saveImageOffline = async (uuid: string, form: FormData) => {
  const qtd: number = await dbGetTable('storedImages').where({ id: uuid }).count();
  if (qtd > 0) {
    await dbGetTable('storedImages').update(uuid, {
      id: uuid,
      content: form.get('image'),
    });
  } else {
    await dbGetTable('storedImages').put({
      id: uuid,
      content: form.get('image'),
    });
  }
};
vinicius gati
  • 421
  • 5
  • 16

2 Answers2

0

You can edit the functionality of pressing the back button to avoid this problem - see:

How to override ion-back-button action in ionic 4 with angular 7

If the behaviour is normal or not depends on your code. Basically a back button wants to undo all actions, f.e entering another view of the app. Honestly if coded cleanly, database functions will not get reversed. They are saved in there and the application doesn't know how to delete the query.

EDIT: Well the behaviour sounds normal to me. When the user cancels the action the execution will be stopped, so no save is made. Basically you upload a picture to the database. If the upload is canceled the image is never fully uploaded.

I don't know if there's anything you can do to make it always go through in that case. You somehow need to upload the picture, which takes time. If the user cancels that action there is no photo uploaded - and in that case no profile picture. However someone else might know it better than me

SickerDude43
  • 168
  • 8
0

Well i dont know if this is the best approach but i made it work adding a function that calls a function inside a global context, then the saving process i have made throught this context function and it works now.

For now this was the solution, if anyone has a better one i really love to hear.

vinicius gati
  • 421
  • 5
  • 16