Firebase 数据库不 return 重视离线
Firebase Database doesn't return value offline
我正在使用 Firebase 数据库开发一个应用程序,我还希望允许用户在离线时添加条目。想象一下以下情况(一切都离线;它在线上完美运行):
- 用户添加了一个客户端(在主表视图中显示 - 有效)
- 在新添加的客户的详细视图上,他添加了与他的约会。约会在客户详细信息 ViewController 的表格视图中列出。
- 添加约会后,tableview 没有刷新,因为回调没有调用。
在代码中:
navCont.child = userDB.child(entityNameClients).childByAutoId()
navCont.child?.setValue(post)
其中 post 是包含客户值的字典
- 在细节视图控制器的viewDidLoad方法上,我调用了下面的代码:
getUsersDBReference().child(entityNameAppointments).child(clientKey).observe(.value)
。未调用回调(可能是因为其中没有元素)
正在添加约会
var appointment:DatabaseReference
appointment = getUsersDBReference().child(entityNameAppointments).child(clientKey).childByAutoId()
appointment.setValue(appointmentDict)
未调用 2 的回调(--> 并且未使用新约会刷新表视图)
顺便说一句,我在 App Delegates applicationDidLoad
方法中设置了 Database.database().isPersistenceEnabled = true
。
如果我在线添加客户端然后离线,它也有效。
Firebase 支持的回答:
The SDK attempts not to fire Value events with "partial" data. 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 (e.g. 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.
One workaround would be to use ChildAdded (and ChildChanged, etc.) events instead of Value.
我正在使用 Firebase 数据库开发一个应用程序,我还希望允许用户在离线时添加条目。想象一下以下情况(一切都离线;它在线上完美运行):
- 用户添加了一个客户端(在主表视图中显示 - 有效)
- 在新添加的客户的详细视图上,他添加了与他的约会。约会在客户详细信息 ViewController 的表格视图中列出。
- 添加约会后,tableview 没有刷新,因为回调没有调用。
在代码中:
navCont.child = userDB.child(entityNameClients).childByAutoId() navCont.child?.setValue(post)
其中 post 是包含客户值的字典- 在细节视图控制器的viewDidLoad方法上,我调用了下面的代码:
getUsersDBReference().child(entityNameAppointments).child(clientKey).observe(.value)
。未调用回调(可能是因为其中没有元素) 正在添加约会
var appointment:DatabaseReference
appointment = getUsersDBReference().child(entityNameAppointments).child(clientKey).childByAutoId() appointment.setValue(appointmentDict)
未调用 2 的回调(--> 并且未使用新约会刷新表视图)
顺便说一句,我在 App Delegates applicationDidLoad
方法中设置了 Database.database().isPersistenceEnabled = true
。
如果我在线添加客户端然后离线,它也有效。
Firebase 支持的回答:
The SDK attempts not to fire Value events with "partial" data. 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 (e.g. 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.
One workaround would be to use ChildAdded (and ChildChanged, etc.) events instead of Value.