Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

android - Firebase handle sudden loss in connection

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...