Ios 为什么不';我的Firebase存储URL上传到Google云Firestore吗?
我正在尝试允许用户将个人资料照片上载到Firebase存储。图像在我的应用程序注册过程中上传。图像已正确上载到Firebase存储,但下载URL未上载到Google Cloud Firestore 我已经尝试将类型为Ios 为什么不';我的Firebase存储URL上传到Google云Firestore吗?,ios,swift,firebase,google-cloud-firestore,firebase-storage,Ios,Swift,Firebase,Google Cloud Firestore,Firebase Storage,我正在尝试允许用户将个人资料照片上载到Firebase存储。图像在我的应用程序注册过程中上传。图像已正确上载到Firebase存储,但下载URL未上载到Google Cloud Firestore 我已经尝试将类型为String的变量downloadURL更改为保存一个空String。这并没有改变结果 以下是将配置文件照片上载到Firebase存储的代码: func uploadProfilePic() -> String { guard let imageData = prof
String
的变量downloadURL
更改为保存一个空String
。这并没有改变结果
以下是将配置文件照片上载到Firebase存储的代码:
func uploadProfilePic() -> String {
guard let imageData = profilePic?.jpegData(compressionQuality: 0.75) else {
print("Unable to get image data.")
return ""
}
let imageID = UUID().uuidString
let profilePhotosRef = Storage.storage().reference().child("profilePhotos/\(imageID).jpg")
let uploadMetadata = StorageMetadata()
uploadMetadata.contentType = "image/jpeg"
var downloadURL = String()
profilePhotosRef.putData(imageData, metadata: uploadMetadata) { (downloadMetadata, err) in
if let err = err {
self.showAlertWithOK(title: "An Error Occurred", message: "There was an error uploading your profile photo. Try again later in Hostend settings.")
print("An error occurred: \(err.localizedDescription)")
}
profilePhotosRef.downloadURL { (url, err) in
if let url = url {
downloadURL = url.absoluteString
}
}
}
return downloadURL
}
以下是将个人资料照片下载URL上载到Cloud Firestore的代码:
db.collection("users").document(uid).setData(["profile_pic_url": uploadProfilePic(), "name": name, "phone_number": phoneNumber, "fcm_token": token, "timestamp": Date()])
uploadProfilePic()
方法返回一个字符串
我希望图像的下载URL可以上传到Firestore的“profile_pic_URL”下,但这并没有发生。相反,它只是一个空的字符串
,即使映像已成功发送到Firebase存储。对于包含闭包的函数,不能使用return语句,因为函数将在执行闭包之前返回
而是将函数更改为使用完成处理程序,例如
func uploadProfilePic(completion: @escaping (String?, Error?) -> ()) {
然后,一旦获得下载url,就调用处理程序
profilePhotosRef.downloadURL { (url, err) in
completion(url, err)
}
然后,您可以使用此函数填充对Cloud Firestore的调用,如下所示
self.uploadProfilePic() { (url, error) in
guard error....
if let url = url {
// do your upload here
}
}
您是否console.log()
ed返回您的方法uploadProfilePic()
?