【发布时间】:2018-02-15 02:16:39
【问题描述】:
我正在制作一个表格,将 3-4 张照片上传到 Firebase 存储。 上传完所有照片后,我想执行一个 segue
self.performSegue(withIdentifier: "createClubToClubDetail", sender: self)
如何在调用 performSegue 之前检查所有照片是否已上传到 firebase。
在for 循环之后放置上面的代码行不起作用,因为上传照片在不同的线程中运行(?)
let storageRef = Storage.storage().reference(forURL: "xxxxx.appspot.com").child("club_photo").child(clubRef.key)
for p in selectedImage {
let imageData = UIImageJPEGRepresentation(p.value, 0.005)
let storageImgRef = storageRef.child(p.key + ".jpg")
storageImgRef.putData(imageData!, metadata: nil) { (metadata, error) in
if error != nil {
print(error!)
return
}
let downloadURL = metadata?.downloadURL()?.absoluteString
var photoRef: DatabaseReference!
if p.key == "main" {
photoRef = ref.child("photo").child(clubRef.key).child("main")
}
if p.key == "sub1" {
photoRef = ref.child("photo").child(clubRef.key).child("sub1")
}
if p.key == "sub2" {
photoRef = ref.child("photo").child(clubRef.key).child("sub2")
}
if p.key == "sub3" {
photoRef = ref.child("photo").child(clubRef.key).child("sub3")
}
photoRef.setValue(downloadURL)
print("Upload photo: \(p.key)")
}
}
self.performSegue(withIdentifier: "createClubToClubDetail", sender: self)
【问题讨论】:
-
您可以使用
dispatch_group_enter()、dispatch_group_leave()并在dispatch_group_notify()中使用self.performeSegue()。 -
Swift 中的示例:stackoverflow.com/questions/45951107/…。另外为什么要使用多个 if 而不是 if-elseif ?差异可以忽略不计,但在这种情况下,您应该在第一场比赛后休息。
标签: ios swift firebase firebase-realtime-database firebase-storage