- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 2 个引用(指向同一路径),一个用于监听挂单,另一个用于监听已完成的订单。全部针对特定位置,用户可以更改该位置。
我现在使用的机制是这样的:
观察
挂单事件之前,将该 Firebase 引用推送到我的数组中观察
已完成订单的事件之前,将该 Firebase 引用推送到我的数组中我的代码如下所示:
struct Firebase {
private let ORDERS_PATH = "locations-orders"
private static var references = [FIRDatabaseReference]()
static func observePendingOrders(lId: String, f: @escaping (AppState.Order) -> () ) {
print("observing orders for \(ORDERS_PATH)/\(lId)")
let pendingOrdersReference = FIRDatabase.database().reference(withPath: "\(ORDERS_PATH)/\(lId)")
self.references.append(pendingOrdersReference)
pendingOrdersReference
.child(dateKeyToday())
.queryOrdered(byChild: "created_at")
.observe(.childAdded, with: { firebaseSnapshot in
...
print("order received for \(ORDERS_PATH)/\(lId)")
}
)
}
static func fetchCompletedOrders(lId: String, f: @escaping ([AppState.Order]) -> () ) {
print("observing completed orders for \(lId)")
let completedOrdersReference = FIRDatabase.database().reference(withPath: "\(ORDERS_PATH)/\(lId)")
self.references.append(completedOrdersReference)
completedOrdersReference
.child(dateKeyToday())
.queryOrdered(byChild: "status_updated_at")
.observe(.value, with: { firebaseSnapshot in
...
}
)
}
static func removeAllOrderObservers() {
for reference in references {
print("removing reference \(reference)")
reference.removeAllObservers()
}
self.references = []
}
}
但是,当我记录我正在监视的订单的位置时,看起来即使在我切换位置后,旧观察者仍在监听订单。这是我的日志:
observing orders for locations-orders/9RC4H9ZAE457E // first location
observing completed orders for 9RC4H9ZAE457E
removing reference https://mydb-123.firebaseio.com/locations-orders/9RC4H9ZAE457E // switch locations, remove all existing observers for location 9RC4H9ZAE457E
removing reference https://mydb-123.firebaseio.com/locations-orders/9RC4H9ZAE457E
observing orders for locations-orders/1JS53G0TT5ZQD // start listening for orders in new location. notice the new id: 1JS53G0TT5ZQD
observing completed orders for 1JS53G0TT5ZQD
order received for locations-orders/9RC4H9ZAE457E // <-- why is this still showing up?
order received for locations-orders/1JS53G0TT5ZQD
order received for locations-orders/1JS53G0TT5ZQD
order received for locations-orders/9RC4H9ZAE457E // <-- and this one too??
order received for locations-orders/1JS53G0TT5ZQD
order received for locations-orders/1JS53G0TT5ZQD
===更新===我什至尝试过 handle ,但没有用:
struct Firebase {
private static var handles = [UInt]()
private static var references = [FIRDatabaseReference]()
static func observePendingOrders(lId: String, f: @escaping (AppState.Order) -> () ) {
print("observing orders for \(ORDERS_PATH)/\(lId)")
let pendingOrdersReference = FIRDatabase.database().reference(withPath: "\(ORDERS_PATH)/\(lId)")
self.references.append(pendingOrdersReference)
let handle = pendingOrdersReference
.child(dateKeyToday())
.queryOrdered(byChild: "created_at")
.observe(.childAdded, with: { firebaseSnapshot in
...
}
)
handles.append(handle)
}
static func fetchCompletedOrders(lId: String, f: @escaping ([AppState.Order]) -> () ) {
print("observing completed orders for \(ORDERS_PATH)/\(lId)")
let completedOrdersReference = FIRDatabase.database().reference(withPath: "\(ORDERS_PATH)/\(lId)")
self.references.append(completedOrdersReference)
let handle = completedOrdersReference
.child(dateKeyToday())
.queryOrdered(byChild: "status_updated_at")
.observe(.value, with: { firebaseSnapshot in
...
}
)
handles.append(handle)
}
static func removeAllOrderObservers() {
// for reference in references {
// print("removing reference \(reference)")
// reference.removeAllObservers()
// }
//
if !references.isEmpty {
let ref = references[0]
for handle in handles {
print("removing handle \(handle)")
ref.removeObserver(withHandle: handle)
}
self.references = []
}
}
}
最佳答案
我想我可能已经修复了它?所以这是因为我实际上是在该引用的子节点上进行观察,因此删除父节点的所有观察者不会执行任何操作,因为您必须在正在观察的特定节点上删除它。
let pendingOrdersReference = FIRDatabase
.database()
.reference(withPath: "\(ORDERS_PATH)/\(lId)")
.child(dateKeyToday()) // <---- added this thing
这是我的最终解决方案,它似乎有效:
struct Firebase {
private static var references = [FIRDatabaseReference]()
static func observePendingOrders(lId: String, f: @escaping (AppState.Order) -> () ) {
print("observing orders for \(ORDERS_PATH)/\(lId)")
let pendingOrdersReference = FIRDatabase
.database()
.reference(withPath: "\(ORDERS_PATH)/\(lId)")
.child(dateKeyToday())
self.references.append(pendingOrdersReference)
...
}
static func fetchCompletedOrders(lId: String, f: @escaping ([AppState.Order]) -> () ) {
print("observing completed orders for \(ORDERS_PATH)/\(lId)")
let completedOrdersReference = FIRDatabase
.database()
.reference(withPath: "\(ORDERS_PATH)/\(lId)")
.child(dateKeyToday())
self.references.append(completedOrdersReference)
...
}
static func removeAllOrderObservers() {
for reference in references {
print("removing reference \(reference)")
reference.removeAllObservers()
}
self.references = []
}
}
关于swift - Firebase + swift : removeAllObservers not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40772101/
删除观察者时遇到问题;即使在removeAllObservers之后该事件似乎也会触发 这是数据结构 listOfItems Item 1 Key:Value Item
removeAllObservers() 是否仅删除其调用的 DatabaseReference 实例的所有观察者,或指定路径上的任何引用? 例如: MainController.swift let
我有 2 个引用(指向同一路径),一个用于监听挂单,另一个用于监听已完成的订单。全部针对特定位置,用户可以更改该位置。 我现在使用的机制是这样的: 创建一个用于存储 Firebase 引用的数组 在观
假设我有几个: [[NSNotificationCenter defaultCenter] addObserver:self
我看到了一些关于此的问题,但没有一个真正证实我的查询。 如果我在父节点处 .removeAllObservers() ,这是否也递归地删除所有其他可能已附加到所有子节点和子节点等的观察者......递
一段时间以来,我一直在尝试移除观察者,但不知何故我从未成功过。我在这里查看了其他帖子,但找不到原因。 这是我的代码: var ref: DatabaseReference! let timeStamp
我正在寻找类似于 Firebase 的东西 completion block确认我在切换到下一个 ViewController 之前删除了所有 Firebase 回调 ref.removeAllObs
众所周知,在 iOS 中使用 Firebase 时, 每当您在 View Controller 中进行观察时, var o: DatabaseReference? var o2: DatabaseRe
我阅读了其他有关此问题的堆栈溢出问答,但它似乎是一个 tabBarController 问题,我还没有找到任何相关信息。 我有一个带有 3 个选项卡的 tabBarController。 tab1 是
我是一名优秀的程序员,十分优秀!