Firebase处理连接突然丢失 [英] Firebase handle sudden loss in connection

查看:99
本文介绍了Firebase处理连接突然丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当互联网连接突然断开时如何控制Firebase写入请求.为了解释我的问题,我将举一个例子:

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:

示例

让我说我一次写3次:

1)WRITE 1:表示将图像上传到Firebase存储.

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

2)WRITE 2:表示将另一张图片上传到Firebase存储.

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

3)WRITE 3:表示将(WRITE 1)和(WRITE 2)的URL链接存储在实时数据库中.

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

一次执行3次写入的合理方法是根据每个接收者的完整侦听器将它们链接起来

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

类似这样的东西:

 //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.

问题

情况1:如果在WRITE 1上失去连接,则其他写入将无法完成.

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

情况2:如果在WRITE 2上失去连接,则最后一次写入将不会执行,最终我将得到无用的文件.因为我无法将它们发送到数据库.

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.

情况3:如果在WRITE 3上失去连接,那么我将无法显示消息.

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

问题

因为这似乎是解决该问题的唯一方法.能不能提供一种方法或解决方案,我不想因为数据库结构不完整而导致应用崩溃.

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.

推荐答案

在没有事务的情况下,您需要做一些事情来确保您的应用程序在不可靠的条件下工作.

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

  1. 以最容易防止或检测故障的顺序写入数据.您首先要写入文件,然后才更新数据库.由于您可能触发了数据库记录中的其他进程,因此只有在第3步完成后,它们才会被触发.因此,您已经在执行此操作.

  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.

编写您的阅读代码,以防止可能出现的不完整数据.例如:即使URL已进入数据库,也不要假设文件正确或存在.而且,当您以单独的读取操作从数据库读取多个节点时,请按照最容易检测不完整或损坏的数据的顺序读取它们,并在这种情况下跳过该项目.

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.

运行常规批处理以清理孤立的数据.我会承认,这样做通常不值得,但是保持卫生是保持数据尽可能正确的前提.

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.

这篇关于Firebase处理连接突然丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆