问题
I am trying to save an image in storage, get the URL for that image and then stop the URL in a field in the database. The problem is that the image gets saved, but not the information for the document in the database, I have tried delaying the storage part but nothing works. I am not sure if the url is the issue or not, it seems if the image is already stored, then a re save of the document works. Here is my function: any help or guidance would be appreciated (I am a new to coding)
@IBAction func submitButtonTapped(_ sender: Any) {
let db = Firestore.firestore()
if let userName = Auth.auth().currentUser?.email {
print(userName)
func validateFields() -> String? {
//Check that all fields are filled in
if mustangNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || blmTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || roundedUpTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || bioTextView.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
return "Please fill in all fields"
}
return nil
}
let error = validateFields()
if error != nil {
// There is something wrong with the fields, show the error message
let e = error
print("Error in data, \(String(describing: e))")
} else {
//Create cleaned version of the data
var mustangName = mustangNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let blm = blmTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let roundedUp = roundedUpTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let bio2 = bioTextView.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let storageRef = Storage.storage().reference().child("Profiles/\(mustangName)")
if let uploadData = self.profileImageView.image!.pngData() {
let metaDataForImage = StorageMetadata()
metaDataForImage.contentType = "image/png"
storageRef.putData(uploadData, metadata: metaDataForImage, completion: { (metadata, error) in
if error != nil {
print(error!)
return
}
print(metaDataForImage)
} )
storageRef.downloadURL { (url, error) in
guard let downloadURL = url?.absoluteString else {
return
}
if url?.absoluteString != "" {
return
} else {
}
print(url!)
print("got url")
db.collection("Profile").document(mustangName).setData(["mustangName": mustangName,"blm": blm, "roundedUp": roundedUp, "bio2": bio2, "userName": userName, "profileImageURL": url?.absoluteString as Any]) {(error) in
if let e = error {
print("Error saving user data to firestore, \(e)")
} else {
print("Successfully saved data for: \(userName)")
}
}
func showError(_ message: String){
self.errorLabel.text = message
self.errorLabel.alpha = 1
}
}
}
}
}
//close profile window and go to home screen
let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
self.view.window?.rootViewController = homeViewController
self.view.window?.makeKeyAndVisible()
}
回答1:
I highly recommend always formatting your code with a code formatter, as it makes it much easier to see the problem.
In this case, you need to move the code that gets the download URL and writes it to Firestore into the completion handler that runs when the uploading of the file complets:
let storageRef = Storage.storage().reference().child("Profiles/\(mustangName)")
if let uploadData = self.profileImageView.image!.pngData() {
let metaDataForImage = StorageMetadata()
metaDataForImage.contentType = "image/png"
storageRef.putData(uploadData, metadata: metaDataForImage, completion: { (metadata, error) in
if error != nil {
print(error!)
return
}
print(metaDataForImage)
storageRef.downloadURL { (url, error) in
guard let downloadURL = url?.absoluteString else {
return
}
if url?.absoluteString != "" {
return
}
else {
}
print(url!)
print("got url")
db.collection("Profile").document(mustangName).setData(["mustangName": mustangName,"blm": blm, "roundedUp": roundedUp, "bio2": bio2, "userName": userName, "profileImageURL": url?.absoluteString as Any]) { (error) in
if let e = error {
print("Error saving user data to firestore, \(e)")
}
else {
print("Successfully saved data for: \(userName)")
}
}
func showError(_ message: String){
self.errorLabel.text = message
self.errorLabel.alpha = 1
}
}
})
}
来源:https://stackoverflow.com/questions/61437031/problem-with-firestore-database-and-storage