【发布时间】:2021-03-27 19:13:38
【问题描述】:
我在一个单独的 swift 文件中有以下函数,我用它来对数据进行 Firebase 调用:
func fetchPosts(data: inout [Post]) {
postRef.observe(DataEventType.value) { (snapshot) in
data.removeAll() // ***Error thrown here***
if snapshot.value is NSNull {
} else {
var counter: UInt = 0
for item in snapshot.children {
let userID = (item as! DataSnapshot).key
self.postRef.child(userID).observe(DataEventType.value) { (snap) in
counter = counter + 1
for child in snap.children {
if let snapshot = child as? DataSnapshot,
let post = Post(snapshot: snapshot) {
data.append(post) // ***Error thrown here***
}
if (counter == snapshot.childrenCount) {
data = data.sorted(by: { $0.id > $1.id }) // ***Error thrown here***
}
}
}
}
}
}
}
在我看来,我有以下几点:
@State var posts: [Post] = [] // which is the place holder for the posts data
然后我有以下电话
func fetchPosts() {
postStore.fetchPosts(data: &posts)
}
调用上面的函数并通过引用传递[Post]数组
我的问题是我在上述函数中收到以下错误Escaping closure captures 'inout' parameter 'data',我无法弄清楚我需要做什么来解决它!?
有没有人遇到过这个错误,你会怎么解决它!?
【问题讨论】:
标签: ios firebase firebase-realtime-database swiftui