swift - 在 Firebase 中一段时间​​后更改值

标签 swift firebase

我有一个大厅,我希望用户在其中保持同步。因此,当用户在应用程序运行时关闭互联网时,他应该被删除。我知道 Firebase 不支持服务器端编码,因此编码需要在客户端进行。 How to delete firebase data after "n" days的答案和 Delete firebase data older than 2 hours不要回答这个问题,因为他们希望用户在线并且他们有互联网连接。所以我的问题是是否可以在没有互联网时删除用户?我想也许让用户每 5 秒更新一次值是个好主意,当更新未完成时,该大厅中的其他用户会删除播放器。这种方式并不好,因为每个玩家每 5 秒需要检索和上传大量数据。解决这个问题的最佳方法是什么?

编辑:简而言之,假设每个用户都有一张图片。用户连接时图像应为绿色,断开连接时图像应为灰色。

编辑2:想了想,在客户端服务器上准确呈现连接的用户确实很难。这就是为什么,如果没有人有不同的解决方案,我应该添加另一个可以执行服务器端代码的服务器。由于服务器较多,我想知道我应该使用哪个服务器。服务器应该运行一个简单的功能,它只检查用户是否连接或断开连接,并且可以与 Firebase 通信。如果我是正确的,它应该是这样的:servers

但服务器也需要直接与用户通信。我完全不知道从哪里开始。

最佳答案

如果我没有完全错的话,你应该可以使用 onDisconnect。

来自 Firebase,文档:

How onDisconnect:Works: When an onDisconnect() operation is established, it lives on the Firebase Realtime Database server. The server checks security to make sure the user can perform the write event requested, and informs the client if it is invalid. The server then monitors the connection. If at any point it times out, or is actively closed by the client, the server checks security a second time (to make sure the operation is still valid) and then invokes the event.

在生产应用中,我使用 onDisconnectRemoveValue,当我关闭应用时,用户将自己从大厅中移除。不确定将设备置于飞行模式时它是如何工作的,但从文档来看应该没有问题。

有一件事:当你最好在真实设备上测试它时,模拟器在关闭和打开它时会出现问题,至少我安装的是这样。

编辑:所以当您将设备置于飞行模式时我检查了 onDisconnect 并且它可以工作!问题是,它会在大约 1:30 分钟内删除用户,因此如果您阅读文档或寻求支持,您可能(并且只能)找到一种方法来设置您想要的时间.

关于swift - 在 Firebase 中一段时间​​后更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42527990/

相关文章:

ios - 在 TableView 上添加 SearchBar

ios - 在 SceneKit 游戏中为 SCNNode 设置动画 SCNConstraint (LookAt) 以使过渡渐进

ios - 索要号码功能错误 canOpenURL : failed for URL: "tel://0478733797" - error: "This app is not allowed to query for scheme tel"

javascript - Firebase功能上的"Error: Client network socket disconnected before secure TLS connection was established"

firebase - 如何配置 Firebase 项目的规则以确保安全?

ios - 在 iOS 应用中使用 L2TP 协议(protocol)实现 VPN

ios - 自定义底部栏过渡到 Swift 中的另一个 View ?

android - 使用通知样式 Firebase 云消息传递发送任意数据

安卓|无法添加版本 15.0.1 的所有 Google 库

Firebase 推送通知在 Firefox 63 中不起作用