简体   繁体   English

是一个流<List<Object> &gt; 从 Firebase 快照在节点的每次更改时返回整个节点? 扑

[英]Is a Stream<List<Object>> from Firebase snapshot returning the entire node at every change in the node? Flutter

I'm starting learning Flutter as I want to port my iOS app to Flutter to also run on Android.我开始学习 Flutter,因为我想将我的 iOS 应用程序移植到 Flutter 以在 Android 上运行。 I use Firebase real time database for the back-end and I saw that the firebase_database package should suit my needs, allowing me to use the db I already have.我使用 Firebase 实时数据库作为后端,我发现firebase_database包应该适合我的需要,允许我使用我已经拥有的数据库。 I'm also learning to use the BLoC pattern but all the bloc tutorials I found are for Firestore and all the Event/State/Bloc/Repository/Streaming is still quite confusing to me right now.我也在学习使用 BLoC 模式,但我发现的所有 bloc 教程都是针对Firestore ,所有的 Event/State/Bloc/Repository/Streaming 现在对我来说仍然很困惑。 What should be the right implementation of the bloc pattern?块模式的正确实现应该是什么?

To a basic level I do understand it and I like it very much, but thinking of a way to implement it for my needs is resulting a bit overwhelming.在基本层面上,我确实理解它并且我非常喜欢它,但是考虑一种根据我的需要实现它的方法会导致有点不知所措。

Following this diagram the 6 connections data flow should be:在此图之后,6 个连接的数据流应该是: 在此处输入图片说明

  1. (UI -> BLoC) An event will be sent to the bloc . (UI -> BLoC) 一个事件将被发送到bloc
  2. (BLoC -> Repository) The Bloc maps the event to a Repository method. (BLoC -> Repository) Bloc将事件映射到Repository方法。
  3. (Repository -> Database) The Repository methods are database Create/Update/Delete methods. (Repository -> Database) Repository方法是数据库的 Create/Update/Delete 方法。
  4. (Database -> Repository) The Repository database Read method gets database Data. (Database -> Repository) Repository数据库读取方法获取数据库数据。
  5. (Repository -> BLoC) The return from Repository database Read method takes the Data into BLoC. (Repository -> BLoC) Repository数据库的返回 Read 方法将 Data 放入 BLoC。
  6. (BLoC -> UI) The BlocProvider uses the Data to rebuild the UI. (BLoC -> UI) BlocProvider 使用数据来重建 UI。

Now what I'm not sure I figured out is the data going back to the UI.现在我不确定我想出的是返回到 UI 的数据。

At step 2 if I map the event to a method that returns the data as a Stream<List<Object>> then via a BlocProvider at step 6 the UI (stateless widget) gets rebuilt accordingly.在第2步,如果我将事件映射到将数据作为Stream<List<Object>>返回的方法,则在第6步通过BlocProvider相应地重建UI (无状态小部件)。 Now, as I see it, that would be a lot of unnecessary repeating data downloads if for any change, the whole node gets downloaded, also Google's bills you on downloads.现在,正如我所见,如果有任何更改,整个节点都会被下载,谷歌也会向您收取下载费用,这将是很多不必要的重复数据下载。

In the Firebase iOS SDK you can attach observers to a node, so you just get a snapshot with the .childAdded / .childRomoved etc etc, and use it to modify your UI.在 Firebase iOS SDK 中,您可以将观察者附加到节点,因此您只需使用.childAdded / .childRomoved等获取快照,并使用它来修改您的 UI。

To avoid useless downloads and keep my account safe, I'd rather make a query on a node with the keepSynced bool set to true ( https://pub.dev/documentation/firebase_database/latest/firebase_database/Query-class.html ).为了避免无用的下载并确保我的帐户安全,我宁愿在 keepSynced bool 设置为 true 的节点上进行查询( https://pub.dev/documentation/firebase_database/latest/firebase_database/Query-class.html ) . This should (as the iOS SDK) return at first firing, a snapshot with the whole node and than snapshots with just new/updated/delete when something changes right?这应该(作为 iOS SDK)在第一次触发时返回,一个包含整个节点的快照,而不是在发生变化时只返回新/更新/删除的快照,对吗?

To implement this way instead, should I use a List<Object> that I update manually when getting the snapshot and a stateful widget?为了实现这种方式,我应该使用在获取快照和有状态小部件时手动更新的List<Object>吗? This is actually how my iOS app works at the moment.这实际上是我的 iOS 应用程序目前的工作方式。 Thank you very much for the help and sorry for the long question, making the switch from Swift is taking it's time.非常感谢您的帮助,对于这么长的问题很抱歉,从 Swift 切换需要时间。

Firebase listeners only transfer the minimal amount of data that actually changed at the node being listened to. Firebase 侦听器仅传输在正在侦听的节点上实际更改的最少量数据。 It does NOT transfer the entire node and all of its children each time anything changes.每次发生任何变化时,它都不会传输整个节点及其所有子节点。 So, it's not as expensive as you're imagining it to be.所以,它并不像你想象的那么贵。

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

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