2

I want to know how I can control my firebase write requests when internet connection is lost suddenly. To explain my problem I will give this example:

Example

lets say I am doing 3 writes at a time:

1) WRITE 1 : represents uploading image to firebase storage.

2) WRITE 2 : represents uploading another image to firebase storage.

3) WRITE 3 : represents storing the url links of (WRITE 1) and (WRITE 2) in the real time database.

The reasonable approach to do the 3 writes at one time is to chain them according to the complete listener of each

something like this:

 //WRITE 1 

 storageref.putfile(image1).addOnCompleteListener(new OnCompleteListener{

     //get link of this write and do WRITE 2 

      //WRITE 2 

       storage ref.putfile(image2).addOnCompleteListener(new OnCompleteListener{

          //get the link of this write and do WRITE 3

         // WRITE 3 
           Map links=new HashMap();
           links.put("image1", link1);
           links.put("image2", link2);  
           databaseref.child("images").setValue(links).addOnCompleteListener(new OnCompleteListener{

             //show a message

        });

  });
  });

As you can see the only way to accomplish this is to chain them together so when one write completes the other executes and so on.

The problem

case 1: if connection is lost on WRITE 1, then other writes won't finish.

case 2: if connection is lost on WRITE 2, then last write won't execute and I end up with useless files on storage. Because I won't be able to send them to the database.

case 3: if connection is lost on WRITE 3 , then I won't be able to show message.

Question

as it seems that this is the only way to go about that. Can you please give a method or a solution to go about this, I don't want to end up with my app crashing due to incomplete database structure.

Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22

1 Answers1

3

In the absence of transactions, you'll need to do a few things to ensure your apps work in unreliable conditions.

  1. Write data in the order that makes it easiest to prevent or detect failures. You're writing the files first and only then update the database. Since it's likely that you trigger your other processes on the database record, they won't get triggered until step 3 completes. So you're already doing this.

  2. Write your reading code to be robust against possible incomplete data. For example: don't assume that the files are correct, or even present, even when the URL has made it into the database. And when you're reading multiple nodes from the database in separate read actions, read them in the order that makes it easiest to detect incomplete or corrupt data and skip the item in such cases.

  3. Run regular batch processes to clean up orphaned data. I'll admit that quite often this is not worth the cost, but it is a matter of good hygiene to keep the data as correct as possible.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • So uncertainty in data being corrupted is always expected? And there is no possible way to stop a write process from completing? What I mean is that at the moment that put file is called on a certain storage reference it can never be stopped? – Hasan Bou Taam Nov 16 '17 at 05:34
  • A file upload that is in progress can be cancelled. See https://firebase.google.com/docs/storage/android/upload-files#manage_uploads – Frank van Puffelen Nov 16 '17 at 05:38
  • And does this apply to database writes? – Hasan Bou Taam Nov 16 '17 at 05:39
  • I don't want to go off topic here, but I must say that for each write in firebase wether it was a file upload or database write ....Oncomplete always is the one waited for , while Onfailure is never triggered. – Hasan Bou Taam Nov 16 '17 at 05:43
  • I mean if my data never reaches database ---> then OnComplete never triggers ------> and also on failure doesn't trigger. – Hasan Bou Taam Nov 16 '17 at 05:46