问题描述
我正在使用Firebase once()
方法在React Native移动应用程序中检索值.问题是,如果电话处于脱机状态,则once()
永不返回.文档说ref.off()
方法应该取消回调,但这似乎行不通.
I am using Firebase once()
method to retrieve a value in a React Native mobile app. Trouble is if the phone is offline, once()
never returns. Documentation says that ref.off()
method should cancel callbacks, but that doesn't seem to work.
我猜想off()
不适用于once()
回调.如果是这样,我将如何创建超时?
I'm guessing that off()
doesn't apply to once()
callbacks. If so how would I create a timeout?
推荐答案
在任何平台上处理Firebase Realtime Database API时,都没有超时这样的概念.除非查询表示这样的特殊位置.
There's no such concept as a timeout when dealing with Firebase Realtime Database APIs on any platform. Clients are generally not expected to know or care if they're online or offline, except by querying a special location that indicates as such.
如果要为查询实现自己的超时(针对Web),请使用setTimeout
强制一些代码在一段时间后执行.您必须小心地在数据库回调和setTimeout
回调之间进行协调,以便如果一个触发,则另一个触发.
If you want to implement your own timeout for a query (for the web), use setTimeout
to force some code to execute after some amount of time. You'll have to be careful to coordinate between your database callback and the setTimeout
callback such that if one triggers, the other one does not.
这篇关于Firebase Once()超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!