我已经更新了我的firebase代码,已经将数据保存到数据库并保存到存储中,但是我的downloadURL返回nil。我不确定为什么我的downloadURL返回零,因为一切都正确保存了。可能非常明显,我忽略了它……它发生了大声笑
let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
let usersRef = Database.database().reference().child("Businesses")
let databaseRef = Database.database().reference()
let imageName = NSUUID().uuidString
let photoRef = photosRef.child("\(uid)")
let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
if let error = error {
print("there was an error")
print(error.localizedDescription)
return
} else {
// store downloadURL
photoRef.downloadURL(completion: {(url, error) in
if error != nil {
let downloadURL = url?.absoluteString
print(downloadURL)
print("HERE#####################################")
let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]
// store downloadURL at database
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
if error != nil {
print("error saving post in db")
} else {
// reset caption field
self.descriptionTextView.text = ""
// reset placeholder image
self.imageView.image = UIImage(named: "filterPlaceholder")
MBProgressHUD.hide(for: self.view, animated: true)
let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
self.present(viewConrolller, animated: true, completion: nil)
}
}
} else {
print(error!.localizedDescription)
print("error")
return
}
})
}
}
容易犯错
我没有访问实际的图像,这就是为什么它返回nil
photoRef.child("\(imageName)").downloadURL(completion: { (url, error) in
答案 0 :(得分:1)
您的if语句在photoRef.downloadURL
的完成处理程序中存在缺陷,如果downloadURL
的值表示存在网络错误,则尝试使用error
。
photoRef.downloadURL(completion: {(url, error) in
if let downloadURL = url, error == nil {...
答案 1 :(得分:0)
只有执行以下操作,才会执行ref.downloadURL完成处理程序:
只需将podfile中的“ FirebaseInstanceID”修复为版本“ 2.0.0”,将执行putData方法。在podfile中看起来像这样: pod'FirebaseInstanceID','2.0.0'
我正在使用Xcode 10.2和Swift 5,该技巧仍然有效