Firestore:使用 SnapshotListener 和 DispatchGroup 进行异步函数的调用会导致崩溃



我在将DispatchGroup(此处推荐)与FireStore snapshotListener一起使用时遇到问题

在我的示例中,我有两个函数。第一个由视图控制器调用,应返回要在视图中显示的对象数组。 第二个是从FireStore获取每个数组成员的子对象的函数。它们都必须异步执行。第二个应该循环调用。

因此,我使用 DispatchGroup 等到第二个函数的所有执行完成才能调用 UI 更新。这是我的代码(见注释部分):

/// Async function returns all tables with active sessions (if any)
class func getTablesWithActiveSessionsAsync(completion: @escaping ([Table], Error?) -> Void) {
let tablesCollection = userData
.collection("Tables")
.order(by: "name", descending: false)
tablesCollection.addSnapshotListener { (snapshot, error) in
var tables = [Table]()
if let error = error {
completion (tables, error)
}
if let snapshot = snapshot {
for document in snapshot.documents {
let data = document.data()
let firebaseID = document.documentID
let tableName = data["name"] as! String
let tableCapacity = data["capacity"] as! Int16
let table = Table(firebaseID: firebaseID, tableName: tableName, tableCapacity: tableCapacity)
tables.append(table)
}
}
// Get active sessions for each table.
// Run completion only when the last one is processed.
let dispatchGroup = DispatchGroup()
for table in tables {
dispatchGroup.enter()
DBQuery.getActiveTableSessionAsync(forTable: table, completion: { (tableSession, error) in
if let error = error {
completion([], error)
return
}
table.tableSession = tableSession
dispatchGroup.leave()
})
}
dispatchGroup.notify(queue: DispatchQueue.main) {
completion(tables, nil)
}
}
}
/// Async function returns table session for table or nil if no active session is opened.
class func getActiveTableSessionAsync (forTable table: Table, completion: @escaping (TableSession?, Error?) -> Void) {
let tableSessionCollection = userData
.collection("Tables")
.document(table.firebaseID!)
.collection("ActiveSessions")
tableSessionCollection.addSnapshotListener { (snapshot, error) in
if let error = error {
completion(nil, error)
return
}
if let snapshot = snapshot {
guard snapshot.documents.count != 0 else { completion(nil, error); return }
// some other code
}
completion(nil,nil)
}
}

一切正常,直到由于在第二个函数中使用快照侦听器而更改快照的那一刻。更改数据时,将调用以下闭包:

DBQuery.getActiveTableSessionAsync(forTable: table, completion: { (tableSession, error) in
if let error = error {
completion([], error)
return
}
table.tableSession = tableSession
dispatchGroup.leave()
})

它在 dispatchGroup.leave() 步骤中失败,因为目前组是空的。

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

所有 dispatchGroup.enter() 和 dispatchGroup.leave() 都已完成此步骤。而这个闭包是由听众单独调用的。

我试图找到如何检查调度组是否为空以不调用 leave() 方法的方法。但没有找到任何本地解决方案。 我找到的唯一类似的解决方案是以下答案。但它看起来太笨拙了,不确定是否能正常工作。

有没有办法检查调度组是否为空?根据这个答案,没有办法。但在过去的两年里可能发生了一些变化。

有没有其他方法可以解决此问题并保持快照侦听器的位置?

现在我实现了某种解决方法 - 使用计数器。 我不觉得这是最好的解决方案,但至少现在工作。

// Get active sessions for each table.
// Run completion only when the last one is processed.
var counter = tables.count
for table in tables {
DBQuery.getActiveTableSessionAsync(forTable: table, completion: { (tableSession, error) in
if let error = error {
completion([], error)
return
}
table.tableSession = tableSession
counter = counter - 1
if (counter <= 0) {
completion(tables, nil)
}
})
}

最新更新