如果用户的互联网关闭,如何更改数据库值 [英] How to change database value if user's internet is switched off

查看:63
本文介绍了如果用户的互联网关闭,如何更改数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我一直在尝试显示用户的在线/离线状态.为此,我进行了一个注册活动,他们在此进行注册,并且他们的信息保存在Firebase中,如果他们退出了活动,我有覆盖其onstop方法并将值设置为离线...但是,如果用户突然失去Internet连接,它将仍然显示为在线..我无法将其更改为离线,因为需要Internet来更改数据库,而使用却没有没有互联网...所以我该如何将数据库值设置为脱机...我在这方面搜索了很多东西,但没有找到任何东西...任何人都可以帮帮我

For the past few days i've been trying to show the online/offline status of a user.. For this i have a register activity where they register and their info gets saved in firebase and if they exit an activity i have overriden its onstop method and made the value to set to offline... but if the user suddenly loses internet connection it still shows online.. i cant change it to offline because internet is needed to make a change in the database and the use doesn't have internet... SO how do i set the database value to offline... i googled quite some stuff about this but didnt find anything... Can anyone please help me out please

我的代码

    @Override
protected void onStart() {
    super.onStart();
    fetchData();
//        mDatabaseReference.child("UserData").child(UID).child("Online").setValue("True");
}
@Override
protected void onStop() {
    super.onStop();
    fetchData();
//        mDatabaseReference.child("UserData").child(UID).child("Online").setValue(false);
}

推荐答案

您要尝试做的事情称为在线状态系统. Firebase数据库具有特殊的API来允许此操作:onDisconnect().将处理程序附加到onDisconnect()时,当服务器检测到客户端已断开连接时,指定的写操作将在服务器上执行.

What you're trying to do is known as a presence system. The Firebase Database has a special API to allow this: onDisconnect(). When you attach a handler to onDisconnect(), the write operation you specify will be executed on the server when that server detects that the client has disconnected.

有关管理状态的文档中:

这是使用onDisconnect原语在断开连接时写入数据的简单示例:

Here is a simple example of writing data upon disconnection by using the onDisconnect primitive:

DatabaseRef presenceRef = FirebaseDatabase.getInstance().getReference("disconnectmessage");
// Write a string when this client loses connection
presenceRef.onDisconnect().setValue("I disconnected!");

在您的情况下,这可能很简单:

In your case this could be as simple as:

protected void onStart() {
    super.onStart();
    fetchData();
    DatabaseReference onlineRef = mDatabaseReference.child("UserData").child(UID).child("Online");
    onlineRef.setValue("True");
    onlineRef.onDisconnect().setValue("False");
}

请注意,这将在简单情况下起作用,但是例如在您的连接快速切换时,将开始出现问题.在这种情况下,服务器检测到客户端消失的时间可能更长(因为这可能取决于套接字超时),而不是客户端重新连接,从而导致无效的False.

Note that this will work in simple cases, but will start to have problems for example when your connection toggles rapidly. In that case it may take the server longer to detect that the client disappears (since this may depends on the socket timing out) than it takes the client to reconnect, resulting in an invalid False.

为更好地处理这些情况,请查看示例存在文档中的系统,它对边缘情况的处理更为精细.

To handle these situations better, check out the sample presence system in the documentation, which has more elaborate handling of edge cases.

这篇关于如果用户的互联网关闭,如何更改数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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