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

查看:25
本文介绍了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 次:

lets say I am doing 3 writes at a time:

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天全站免登陆