简体   繁体   English

Swift/Firestore:完成不会被调用

[英]Swift/Firestore: Completion doesn't get called

I'm trying to create a "Profile" collection in Firestore in order to store more data on my users than just their email/name.我正在尝试在 Firestore 中创建一个“个人资料”集合,以便在我的用户上存储更多数据,而不仅仅是他们的电子邮件/姓名。 I'm stuck with creating this document and uploading the profile picture they choose (as an URL).我坚持创建此文档并上传他们选择的个人资料图片(作为 URL)。

Here is the function called when they click on the "Register" button:这是当他们单击“注册”按钮时调用的函数:

func register() {
            Auth.auth().createUser(withEmail: self.email, password: self.pass) { (res, err) in
                if err != nil {
                    self.error = err!.localizedDescription
                    self.alert.toggle()
                    return
                }
                // Success registering a new user
                guard let userUID = res?.user.uid else { return }

                uploadImageToFirestore(id: userUID, image: myImage) {
                    print("SUCCESS")
                    self.imageURL = downloadImageFromFirestore(id: userUID)
                    self.createUserDocument(id: userUID, imgURL: self.imageURL)
                }
        }

First step is uploading picture on Firebase Storage using the uploadImageToFirestore function and I tried using a completion handler to wait before calling the next 2 functions:第一步是使用uploadImageToFirestore函数在 Firebase 存储上上传图片,我尝试使用完成处理程序在调用接下来的两个函数之前等待:

    func uploadImageToFirestore(id: String, image: UIImage, completion: () -> Void) {
    let storageRef = storage.reference().child("images/\(id)/image.jpg").putData(image.jpegData(compressionQuality: 0.35)!, metadata: nil) { (_, err) in
        if err != nil {
            print((err?.localizedDescription)!)
            return
        }
        print("Success uploading picture to Firestore")
    }
}

Second step is downloading the newly uploaded image on Firebase Storage to get the URL:第二步是在 Firebase 存储上下载新上传的图像以获取 URL:

    func downloadImageFromFirestore(id: String) -> String {
    let storageRef = storage.reference().child("images/\(id)/image.jpg")
    storageRef.downloadURL { url, error in
        if error != nil {
            print("DEBUG: \((error?.localizedDescription)!)")
            return
        }
        print("Success downloading picture from Firestore")
        self.imageURL = "\(url!)"
    }
    return self.imageURL
}

Third step is creating the Profile collection in Firestore with the ImageURL:第三步是在 Firestore 中使用 ImageURL 创建 Profile 集合:

    func createUserDocument(id: String, imgURL: String) {
    db.collection("profiles").document(id).setData([
        "name": name,
        "surname": surname,
        "email": email,
        "shelter": isMember == true ? shelters[selectedShelter] : shelters[0],
        "photoURL": imgURL,
        "reputation": 0,
        "uuid": id
    ])
    { err in
        if let error = err {
            print("Error ading document: \(error)")
        } else {
            print("New profile document created in Firestore")
        }
    }
}

THE PROBLEM问题

The problem I face is that in my "Register" function, the completion block of uploadImageToFirestore is never called, thus the function createUserDocument neither.我面临的问题是,在我的“注册”函数中, uploadImageToFirestore的完成块从未被调用,因此函数 createUserDocument 也不被调用。

Is this the best way to achieve what I want (aka creating a profile document with the imageURL of the picture they just choose while registering)?这是实现我想要的最佳方式吗(也就是使用他们在注册时选择的图片的 imageURL 创建配置文件)? Why is my completion block not called?为什么我的完成块没有被调用? (I don't see the "SUCCESS" printed in my console). (我没有在控制台中看到“成功”字样)。

Thank you for your help!感谢您的帮助!

Kind regards, Jihaysse亲切的问候,吉海斯

You need to call the completion handler closure that you are passing to uploadImageToFirestore .您需要调用传递给uploadImageToFirestore的完成处理程序闭包。

You should probably also pass the error , if any, so that you can handle it.您可能还应该传递error (如果有),以便您可以处理它。

func uploadImageToFirestore(id: String, image: UIImage, completion: (Error?) -> Void) {
    let storageRef = storage.reference().child("images/\(id)/image.jpg").putData(image.jpegData(compressionQuality: 0.35)!, metadata: nil) { (_, err) in
        completion(err)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM