Async Future Promise在Swift 5中不工作



我正在尝试实现对函数loadUserFromFirebase的异步调用,并根据用户属性填充并返回一个数组,然后我使用该数组写入firebase集合。

我试图使用结合框架从Swift使用未来和承诺,但出于某种原因,receiveCompletion和receiveValue不被调用,因此我得到一个空数组从我的异步函数调用。

下面是我的代码:
var cancellable = Set<AnyCancellable>()
func loadUserFromFirebase(groupUserIds: [String], handler: @escaping ([groupMate]) -> Void) {

var groupmateID = 0
var groupMates : [groupMate] = []
print("groupUserIds: (groupUserIds)" )

for groupUserUID in groupUserIds {
print("groupUserUID: (groupUserUID)" )

let future = Future<groupMate, Never> { promise in
self.ref.collection("Users").document(groupUserUID).getDocument(){

(friendDocument, err) in

if let err = err {
print("Error getting documents (err)")
} else {
//  print("friendDocument: (String(describing: friendDocument?.data()))" )
let groupUsername = (friendDocument?.data()?["username"]) as! String
let groupUID = (friendDocument?.data()?["uid"]) as! String
let groupName = (friendDocument?.data()?["name"]) as! String
let groupPic = (friendDocument?.data()?["imageurl"]) as! String
promise(.success(groupMate(id: groupmateID, uid: groupUID , name: groupName , username: groupUsername, pic: groupPic)))
}

groupmateID += 1
}
}
print("in receiveCompletion")
future.sink(receiveCompletion: { completion in
print("in receiveCompletion")
print(1, completion)
switch completion {
case .failure(let error):
print(3, error)
handler([])
return
case .finished:
break
}
},
receiveValue: {
print("in receiveValue")
groupMates.append($0)
print(groupMates)
handler(groupMates)
}).store(in: &cancellable)
}
}


func creategroup(groupName: String){
addedTogroupUsers.append(self.uid)

print("here111")
loadUserFromFirebase(groupUserIds: addedTogroupUsers) { groupMates in
print("here222")
let groupData: [String: Any] = [
"groupName": "(groupName)",
"groupmates": groupMates
]
print("here333 (groupData)")
print("groupMates are (self.groupMates)")


var groupref: DocumentReference? = nil
groupref = self.ref.collection("groups").addDocument(data: groupData) { err in
if let err = err {
print("Error adding document: (err)")
} else {
print("Document added with ID: (groupref!.documentID)")
for addedgroupUser in self.addedTogroupUsers {
self.ref.collection("Users").document(addedgroupUser).updateData([
"groups": FieldValue.arrayUnion([groupref!.documentID])
])
}
}
}
print("groupName is (groupName) and addedTogroup are (self.addedTogroupUsers)")
}
}

我想看看是否AnyCancellable是要走的路,但因为我使用未来的承诺链数组,我不知道如何实现它。请让我知道你如何解决这个问题,以便数组确实得到填充,因为文档确实存在,方法调用工作中的打印,但createGroup函数中的groupMates数组打印一个空数组之后。谢谢!

编辑:添加AnyCancallable代码随着完成处理程序的建议

处理异步函数可能很棘手。你得到一个空数组,因为你返回太早,在loadUserFromFirebase。使用旧式闭包尝试这种方法(未经测试):

func loadUserFromFirebase(groupUserIds: [String], handler: @escaping ([groupMate]) -> Void) { // <-- here
var groupmateID = 0
var groupMates : [String] = []
print("groupUserIds: (groupUserIds)" )

for groupUserUID in groupUserIds {
print("groupUserUID: (groupUserUID)" )

let future = Future<groupMate, Never> { promise in
self.ref.collection("Users").document(groupUserUID).getDocument(){ (friendDocument, err) in
if let err = err {
print("Error getting documents (err)")
} else {
print("friendDocument: (String(describing: friendDocument?.data()))" )

let groupUsername = (friendDocument?.data()?["username"]) as! String
let groupUID = (friendDocument?.data()?["uid"]) as! String
let groupName = (friendDocument?.data()?["name"]) as! String
let groupPic = (friendDocument?.data()?["imageurl"]) as! String

promise(.success(groupMate(id: groupmateID, uid: groupUID , name: groupName , username: groupUsername, pic: groupPic)))
}
groupmateID += 1
}
}

future.sink(receiveCompletion: { completion in
print("in receiveCompletion")
print(1, completion)
switch completion {
case .failure(let error):
print(3, error)
handler([])  // <-- here
return          // <-- here
case .finished:
break
}
},
receiveValue: {
print("in receiveValue")
groupMates.append($0)
print(groupMates)
handler(groupMates)  // <-- here
})
}
// <-- do not return here
}

func creategroup(groupName: String) {
addedTogroupUsers.append(self.uid)

// -- here wait until you get the data
loadUserFromFirebase(groupUserIds: addedTogroupUsers) { groupMates in

let groupData: [String: Any] = [
"groupName": "(groupName)",
"groupmates": groupMates      // <-- here
]

print("groupMates are (self.groupMates)")

var groupref: DocumentReference? = nil
groupref = ref.collection("groups").addDocument(data: groupData) { err in
if let err = err {
print("Error adding document: (err)")
} else {
print("Document added with ID: (groupref!.documentID)")
for addedgroupUser in self.addedTogroupUsers {
self.ref.collection("Users").document(addedgroupUser).updateData([
"groups": FieldValue.arrayUnion([groupref!.documentID])
])
}
}
}
print("groupName is (groupName) and addedTogroup are (addedTogroupUsers)")
}

}
注意,如果你的目标是ios 15, macos 12,如果你使用swift 5.5的async/await/task功能,你会得到更好的服务。它们真的很好用。

编辑:尝试返回所有结果

func loadUserFromFirebase(groupUserIds: [String], handler: @escaping ([groupMate]) -> Void) {
var groupmateID = 0
var groupMates : [String] = []
print("groupUserIds: (groupUserIds)" )

var arr: [Future<groupMate, Never>] = [Future<groupMate, Never>]()

var cancellable = Set<AnyCancellable>()

for groupUserUID in groupUserIds {
print("groupUserUID: (groupUserUID)" )

let future = Future<groupMate, Never> { promise in
self.ref.collection("Users").document(groupUserUID).getDocument(){ (friendDocument, err) in
if let err = err {
print("Error getting documents (err)")
} else {
print("friendDocument: (String(describing: friendDocument?.data()))" )

let groupUsername = (friendDocument?.data()?["username"]) as! String
let groupUID = (friendDocument?.data()?["uid"]) as! String
let groupName = (friendDocument?.data()?["name"]) as! String
let groupPic = (friendDocument?.data()?["imageurl"]) as! String

promise(.success(groupMate(id: groupmateID, uid: groupUID , name: groupName , username: groupUsername, pic: groupPic)))
}
groupmateID += 1
}
}
arr.append(future)
}

Publishers.MergeMany(arr)
.collect()
.sink { _ in
print("-----> merging ")
} receiveValue: { value in
print("-----> value: (value)")
groupMates = value  // <--- maybe append?
print(groupMates)
handler(groupMates)
}
.store(in: &cancellable)
}

相关内容

  • 没有找到相关文章

最新更新