[英]Firebase Realtime DB timeout
I have implemented a simple timeout system for requests to my Realtime Database. 我已经为请求实时数据库实现了一个简单的超时系统。
private void makeRequestWithTimeout(final int timeout, final DatabaseReference reference, final OnTimeoutRequestListener listener) {
Thread thread = new Thread() {
boolean connected = false;
boolean exited = false;
@Override
public void run() {
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!exited) {
listener.onSuccess(dataSnapshot);
connected = true;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
if (!exited) {
listener.onFailure(databaseError.toException());
connected = true;
}
}
});
try {
sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!connected) {
listener.onFailure(new Exception("Timeout, request exceeded " + timeout + "ms timeout"));
exited = true;
}
}
};
thread.start();
}
I need this because I want to check if for example a username is already taken, and I don't want to proceed if I can't get a value from the db. 我需要这样做是因为我想检查例如是否已使用用户名,并且如果无法从数据库中获取值,我也不想继续。
Okay so I thought this works pretty good but following scenario doesn't work: 好的,所以我认为这很好用,但是以下情况不起作用:
When I'm in airplane mode and make a request, the timeout exception fires as expected. 当我处于飞行模式并发出请求时,超时异常会按预期触发。 Then I disable airplane mode and make another request.
然后,我禁用飞行模式并提出另一个请求。 I get another timeout exception.
我收到另一个超时异常。
EDIT: this goes on for 2 minutes, until a request is successful again. 编辑:这持续2分钟,直到再次请求成功。
Turning on Database logging, it seems that it doesn't even try to get the value. 打开数据库日志记录,似乎它甚至没有尝试获取该值。
I've also read this . 我也读过这个 。
Is there anything I can do about it? 有什么我可以做的吗?
When I run your code with version 10.0.1, I see the reported behavior except the delay before requests are successful again is only about 20 seconds. 当我使用10.0.1版运行您的代码时,我看到了报告的行为,除了请求再次成功之前的延迟只有大约20秒。
It appears that when a connection becomes available after exiting Airplane Mode, it takes Firebase a short time to detect the change. 似乎当退出“飞行模式”后连接变为可用时,Firebase需要很短的时间来检测到更改。 You can see when Firebase recognizes the change by adding a connection state listener :
您可以通过添加连接状态侦听器查看Firebase何时识别更改:
FirebaseDatabase.getInstance().getReference(".info/connected")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.getValue(Boolean.class)) {
Log.i(TAG, "Firebase CONNECTED");
} else {
Log.i(TAG, "Firebase NOT CONNECTED");
}
}
@Override
public void onCancelled(DatabaseError error) {
Log.e(TAG, "onCancelled: ", error.toException());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.