简体   繁体   English

在 firebase 中执行潜在的无限 while 循环是否安全?

[英]Is it safe to do a potential infinite while loop to write data in firebase?

I'm using firebase in my app and most of the time it writes the values on the firebase entry with this instruction.我在我的应用程序中使用 firebase 并且大多数时候它使用此指令在 firebase 条目上写入值。

ref.setValue(result);

But on some rare occasions value is not written.但在极少数情况下不写入值。

So I have thought about doing this:所以我想过这样做:

boolean finished=false;
[...]  

t=new Thread(new Runnable() {
                        @Override
                        public void run() {
                        while (!finished)
                        {
                            ref.setValue(result, new DatabaseReference.CompletionListener() {
                                @Override
                                public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                                    finished=true;
                                }
                        }
                            });
                            t.sleep(50);
                        }
                    });
                    t.start();

As most of the time the data is written successfully, it looks like even if an error where to happen when wrting data, it would end up being written successfully anyway.由于大多数情况下数据写入成功,看起来即使写入数据时发生错误,最终还是会成功写入。

But maybe this is not enough to ensure that the data ends up being written and it may cause an infinite loop, getting one database error after another.但也许这不足以确保数据最终被写入,并且可能导致无限循环,导致一个接一个的数据库错误。

Is this a good approach to solve the problem?这是解决问题的好方法吗?

I would say no this is not a good solution, a better solution would be a counter and a maximum number of retries say 10 this removes the risk of an infinite loop in the case of constant database errors.我会说不,这不是一个好的解决方案,更好的解决方案是计数器,最大重试次数为 10,这消除了在不断出现数据库错误的情况下出现无限循环的风险。 While still allowing multiple tries to insert the data correctly into the firebase database.虽然仍然允许多次尝试将数据正确插入 firebase 数据库。

The Realtime Database client already runs all network operations in a background thread.实时数据库客户端已在后台线程中运行所有网络操作。 This means that all operations take place without blocking your main thread.这意味着所有操作都不会阻塞您的主线程。 Trying to create a new Thread, does not give any benefits at all.尝试创建一个新线程,根本没有任何好处。 Besides that, trying to use:除此之外,尝试使用:

t.sleep(50);

Might work, if the request takes less than 50 milliseconds.如果请求时间少于 50 毫秒,可能会起作用。 How about 51? 51怎么样? That's not how you should deal with asynchronous operations.这不是你应该如何处理异步操作。 However, the solution is always the same.但是,解决方案总是相同的。 Any code that needs to be used when the write operation completes, needs to be inside the onComplete method, or be called from there.写操作完成时需要使用的任何代码,都需要在onComplete方法中,或者从那里调用。 If you want to learn how to handle asynchronous operations, I recommend you check my answer from the following post:如果你想学习如何处理异步操作,我建议你从下面的帖子中查看我的答案:

If you understand Kotlin, I also recommend you read the following article:如果您了解 Kotlin,我还建议您阅读以下文章:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM