简体   繁体   English

Firebase数据库不会脱机返回值

[英]Firebase Database doesn't return value offline

I'm working on a app with Firebase Database and I also want to allow the user adding entries when he's offline. 我正在开发一个使用Firebase数据库的应用程序,我还希望允许用户在离线时添加条目。 Imagine the following case (everything offline; it works perfectly online): 想象一下以下情况(一切离线;它在网上完美运行):

  1. The user adds a client (shown in a main tableview - works) 用户添加一个客户端(显示在主表视图中 - 工作)
  2. On the detail view of the freshly added client, he adds an appointment with him. 在新近添加的客户的详细视图中,他预约了他。 The appointments are listed in a tableview in the Client Detail ViewController. 约会在Client Detail ViewController的tableview中列出。
  3. When the appointment is added, the tableview isn't refreshed, because the callback doesn't call. 添加约会时,不会刷新tableview,因为回调不会调用。

In code: 在代码中:

  1. navCont.child = userDB.child(entityNameClients).childByAutoId() navCont.child?.setValue(post) where post is a dictionary with the values of the customer navCont.child = userDB.child(entityNameClients).childByAutoId() navCont.child?.setValue(post)其中post是一个包含客户值的字典
  2. On the viewDidLoad Method of the detail view controller, I call the following code: getUsersDBReference().child(entityNameAppointments).child(clientKey).observe(.value) . 在详细视图控制器的viewDidLoad方法中,我调用以下代码: getUsersDBReference().child(entityNameAppointments).child(clientKey).observe(.value) The Callback isn't called (probably because there are no elements in it) 不调用回调(可能是因为其中没有元素)
  3. Adding Appointment 添加约会

     var appointment:DatabaseReference 

    appointment = getUsersDBReference().child(entityNameAppointments).child(clientKey).childByAutoId() appointment.setValue(appointmentDict)

  4. The Callback of 2 isn't called (--> and the tableview not refreshed with the new appointment) 不调用2的回调( - >并且新约会不刷新tableview)

BTW, I set Database.database().isPersistenceEnabled = true in App Delegates applicationDidLoad -method. 顺便说一句,我在App Delegates applicationDidLoad -method中设置了Database.database().isPersistenceEnabled = true And if I add the client online and go offline then, it works too. 如果我在线添加客户端然后离线,那么它也可以。

Answer of Firebase Support: Firebase支持的答案:

The SDK attempts not to fire Value events with "partial" data. SDK尝试不使用“部分”数据触发Value事件。 Since you are only updating a subnode of the location being observed, we don't have "complete" data and so the observer is not fired. 由于您只更新被观察位置的子节点,因此我们没有“完整”数据,因此不会触发观察者。 If the SDK has ever had "complete" data for that node (eg because you overwrote the whole node or because you were previously online and got complete data for it), then it will fire events when you update it. 如果SDK曾经拥有该节点的“完整”数据(例如,因为您覆盖了整个节点,或者因为您以前在线并获得了完整的数据),那么它会在您更新时触发事件。

One workaround would be to use ChildAdded (and ChildChanged, etc.) events instead of Value. 一种解决方法是使用ChildAdded(和ChildChanged等)事件而不是Value。

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

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