我正在考虑从 Firebase 获取数据并使用房间库将其存储在我的手机上。但是,我心里有一个疑问。我想使用 Firebase ChildEventListener
因为我只想在更改或添加子项时才获取数据。但是,我也知道 ChildEventListener 在应用程序第一次启动时会获取所有数据。所以,my question is that in what cases firebase ChildEventListener will fetch whole data and in what cases a single child?
。实际上,了解这一点很重要,因为如果 ChildEventListener 两次获取相同的数据,room 将给出唯一 id 重复错误。
最佳答案
如果您无法控制 ChildEventListener 何时触发,一个简单的策略是在所有情况下都进行覆盖。您可以使用以下代码避免唯一 ID 重复错误:
@Insert(onConflict = OnConflictStrategy.REPLACE)
如果替换不适合您,有 other options .
关于java - Firebase 与 Room 持久性库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51700757/