gpt4 book ai didi

swift - Firebase 上传 URL 到实时数据库

转载 作者:行者123 更新时间:2023-11-28 11:39:19 25 4
gpt4 key购买 nike

我正在尝试将图像 url 从 Firebase 存储上传到实时数据库。

这是代码

  @IBOutlet weak var jobTitle: UITextField!
@IBOutlet weak var companyName: UITextField!
@IBOutlet weak var jobLocation: UITextField!
@IBOutlet weak var ImageView1stPoster: UIImageView!
var imageUploaded = Data()
var URLtoRealtime = ""

func addPost() {
ref.child("Poster").childByAutoId().setValue(["jobName": jobTitle.text as Any,
"companyTitle": companyName.text as Any,
"jobLocation": jobLocation.text as Any,
"firstPoster": URLtoRealtime as Any,
/*,
"timeStamp":[".sv":"timestamp"]*/]
as[String:Any])
}


// DoneButton to submit everthing :)
@IBAction func DoneButton(_ sender: Any) {
uploadImageToFirebase(imageData: imageUploaded)
createAlert(title: "Post has been submitted", message: "Going to home page")
addPost()

}


func uploadImageToFirebase(imageData: Data) {
// References and vars
let StorageRefrenece = Storage.storage().reference()
let currentUser = Auth.auth().currentUser
let posterImageRef = StorageRefrenece.child("posters").child(currentUser!.uid).child("posterOne.jpg")

let uploadMetaData = StorageMetadata()
uploadMetaData.contentType = "image/jpeg"

// putData to put data to the server using MetaData to orignize everthing.
posterImageRef.putData(imageData, metadata: uploadMetaData) { (uploadedImageMeta, error) in
if error != nil {
print("Error Took place \(String(describing: error?.localizedDescription))")
return
} else {
print("metaData of uploaded image \(uploadMetaData)")
}
}

posterImageRef.downloadURL { (url, error) in
if (error != nil) {
// Handle any errors
print(error!.localizedDescription)
print("NOOOPPPEEE")
} else {
// Get the download URL for 'images/stars.jpg'
print("Working Good")
let UrlString = url!.absoluteString
print(UrlString)
self.URLtoRealtime = UrlString
}

}


}

我正在尝试制作 URLString = URLtoRealtime 数据,然后将帖子添加到 firebase。但是发生的事情是它在之前执行了 addPost() 函数self.URLtoRealtime = UrlString

我不知道如何让程序执行 addpost() 函数之前的前一行代码。

最佳答案

当触摸完成按钮时,您将异步上传图像到 firebase 并下载 URL。但是,正如您所指出的,当您addPost 时,该 URL 尚未下载 – 您想要一个接一个地下载。

@IBAction func DoneButton(_ sender: Any) {
uploadImageToFirebase(imageData: imageUploaded) { [weak self] (url, error) in
if let url = url {
createAlert(title: "Post has been submitted", message: "Going to home page")
self?.URLtoRealtime = url.absoluteString
self?.addPost()
} else {
self?.createAlert(title: "Post could not be submitted", message: "Try again")
}
}
}

我们可以在上传方法中添加一个完成参数,以便您可以在 URL 的上传和下载完成后addPost

func uploadImageToFirebase(imageData: Data, completion: @escaping (URL?, Error?) -> ()) {

guard let uid = Auth.auth().currentUser?.uid else { return completion(nil, nil) }

let posterImageRef = Storage.storage().reference(withChild: "posters/\(uid)/posterOne.jpg")
let uploadMetaData = StorageMetadata(dictionary: [ "contentType": "image/jpeg" ])

posterImageRef.putData(imageData, metadata: uploadMetaData) { (metadata, error) in
if let ref = metadata?.storageReference {
ref.downloadURL(completion: { (url, error) in
completion(url, error)
})
} else {
completion(nil, error)
}
}
}

关于swift - Firebase 上传 URL 到实时数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54163147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com