简体   繁体   English

多个 Firebase 数据库子/值侦听器对移动应用程序是否有效?

[英]Are multiple Firebase Database Child/Value Listeners efficient for mobile app?

I've been reading about Firebase Realtime Database for a while now I'm just wondering about the whole idea of event-driven data fetching from database.我一直在阅读有关 Firebase 实时数据库的文章,现在我只是想知道从数据库中获取事件驱动数据的整个想法。 If data is downloaded from the database only when there was a change made to it, then it should be much less demanding on the device resources like battery or Internet connection.如果仅在对数据库进行更改时才从数据库下载数据,则对电池或 Internet 连接等设备资源的要求应该会低得多。 I thought I could remap the whole database fragment I need in my Android activity with my model objects that reflect the structure of my database and update its values with Child/Value listeners.我想我可以使用反映我的数据库结构的模型对象重新映射我在 Android 活动中需要的整个数据库片段,并使用 Child/Value 侦听器更新其值。 Then I would have the whole data I need in a flexible form of POJOs and it would update every time there was a change in the database.然后我会以灵活的 POJO 形式获得我需要的全部数据,并且每次数据库发生变化时它都会更新。 So it is actually a real-time solution.所以它实际上是一个实时解决方案。 But I wonder if such number of listeners would slow down or overload user's device in any terms like battery life or data transfer.但我想知道如此多的听众是否会在电池寿命或数据传输等任何方面减慢或过载用户的设备。

So, would It be a good solution to reflect database state in my Java model objects or should I go another way?那么,在我的 Java 模型对象中反映数据库状态是一个很好的解决方案,还是应该采用另一种方式?

Could anyone resolve my doubts?有人能解开我的疑惑吗? Thanks!谢谢!

The listeners are triggered only when something is changed in the database.仅当数据库中的某些内容发生更改时才会触发侦听器。 So you get data only when needed.因此,您仅在需要时获取数据。 It is expected that your app will lose their socket connections when there is no visible activity.当没有可见的活动时,您的应用程序预计将丢失其套接字连接。 That's the way the Firebase SDK works.这就是 Firebase SDK 的工作方式。 Also, Android will aggressively pause background applications so they don't consume data and battery when the user isn't actively using it.此外,Android 会主动暂停后台应用程序,以便在用户不主动使用它时不会消耗数据和电池。

When you restart an activity for example, the Firebase SDK attempts to restore the websocket that it uses to communicate with the server.例如,当您重新启动 Activity 时,Firebase SDK 会尝试恢复它用来与服务器通信的 websocket。 This websocket is fully managed by the SDK.这个 websocket 完全由 SDK 管理。

Hope it helps.希望能帮助到你。

Firebase.addvalueeventlistener() only work when there is any change or update in database you are requesting. Firebase.addvalueeventlistener() 仅在您请求的数据库中有任何更改或更新时才起作用。 And it is a good way to move with JAVA objects or you can also use HashMaps<>.这是使用 JAVA 对象移动的好方法,或者您也可以使用 HashMaps<>。 As far as there is concern for device battery and performance then instead of requesting whole database you try to get only that part whose change event you wanna track.就设备电池和性能而言,与其请求整个数据库,不如尝试仅获取要跟踪其更改事件的那部分。 And if you also want to have grabbing data on background as Whatsapp you can you Broadcast Receiver for internet and startservice() for where Firebase.addvalueeventListener() used.如果您还想在后台获取数据作为 Whatsapp,您可以使用 Broadcast Receiver for internet 和 startservice() 用于 Firebase.addvalueeventListener() 使用的位置。

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

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