我正在尝试在Firebase完成其下载/上传(例如segue或刷新)后运行一些代码
例如
我具有x3保存功能,所有功能均具有用于更新某些数据(例如文本和图像)的存储和数据库的代码
save1()
save2()
save3()
当执行IBAction时,我希望这些功能运行,如果它们没有错误,则在运行完成时(segue或刷新)执行这些功能,
这3个保存功能当前在IBAction中起作用
@IBAction func saveTap(_ sender: Any) {
save1()
save2()
save3()
}
保存功能如下:
(我检查图像是否已更改,然后开始上传过程)
func save1(){
if image1.image == nil {
let gender = userGender.text
self.databaseRef.child("users").child(gender!).child(Auth.auth().currentUser!.uid).child("images").child("imageOne").removeValue { (error, ref) in
if error != nil {
print(error!)}
}
let imageRef = self.storage.child(Auth.auth().currentUser!.uid).child("image1")
imageRef.delete { error in
if let error = error {
print(error)
} else {
print("Photo 1 image deleted")}
}
} else {
//Firebase child references
let profileImageRef = storage.child(Auth.auth().currentUser!.uid).child("image1")
let metaData = StorageMetadata()
metaData.contentType = "image1/jpeg"
//Firebase child references
//change uiimageview to uiimage for upload
guard let image = image1.image else
{return}
//change uiimageview to uiimage for upload
//Send to firebase storage
profileImageRef.putData(image.jpegData(compressionQuality: 0.1)!, metadata: metaData) { (data, error) in
if error == nil
{print("Photo 1 uploaded to storage")}
else
{print(error?.localizedDescription as Any)}}
//Send to firebase storage
//Update firebase database
profileImageRef.downloadURL(completion: { (url, error) in
if error != nil{
print(error!)
return}
if let profilePhotoUrl = url?.absoluteString{
let newValuesForProfile = profilePhotoUrl
let gender = self.userGender.text
self.databaseRef.child("users").child(gender!).child(Auth.auth().currentUser!.uid).child("images").child("imageOne").setValue(newValuesForProfile, withCompletionBlock: { (error, ref) in
if error != nil{
print(error!)
return}
print("Photo 1 updated in database")})}})
//Update firebase database
}
我需要先完成上传,然后再执行segues,因为下一个视图将刷新到我要上传的已保存数据。
任何帮助都会很棒,已经在这里工作了几个星期:( iv尝试了完成处理程序,但到目前为止还没有运气。
提前谢谢
答案 0 :(得分:2)
我认为dispatchGroup
适合您的情况
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
save1 { dispatchGroup.leave() }
dispatchGroup.enter()
save2 { dispatchGroup.leave() }
dispatchGroup.notify(queue: .main) {
self.perFormSegue//////
}
//
// e.x结构
func save1(completion:@escaping()->()) {
firesCallBack {
if success {
completion()
}
}
}
答案 1 :(得分:0)
您可以像这样检查更改是否用.childChanged保存:
save1()
save2()
save3()
let ref: DatabaseReference!
ref = /YourDirectDatabaseReferenceForTheChangedNode/
ref.observe(.childChanged, with: {(snaphost) -> Void in
}
})
也许您也应该使用UIActivityIndicatorView来显示背景中正在发生的事情。
@IBAction func saveTap(_ sender: Any) {
save1()
save2()
save3()
let activity: UIActivityIndicatorView = UIActivityIndicatorView()
activity.center = self.view.center
activity.hidesWhenStopped = true
activity.activityIndicatorViewStyle = .whiteLarge
self.view.addSubview(activity)
let ref: DatabaseReference!
ref = /YourDirectDatabaseReferenceForTheChangedNode/
ref.observe(.childChanged, with: {(snaphost) -> Void in
activity.stopAnimating()
if UIApplication.shared.isIgnoringInteractionEvents {
UIApplication.shared.endIgnoringInteractionEvents()
}
self.performSegue(withIdentifier: "backhome", sender: self)
}
})
}
将/ YourDirectDatabaseReferenceForTheChangedNode /替换为正确的子项(保存后应更改的内容)。