繁体   English   中英

Swift/Firestore:完成不会被调用

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

我正在尝试在 Firestore 中创建一个“个人资料”集合,以便在我的用户上存储更多数据,而不仅仅是他们的电子邮件/姓名。 我坚持创建此文档并上传他们选择的个人资料图片(作为 URL)。

这是当他们单击“注册”按钮时调用的函数:

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)
                }
        }

第一步是使用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")
    }
}

第二步是在 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
}

第三步是在 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")
        }
    }
}

问题

我面临的问题是,在我的“注册”函数中, uploadImageToFirestore的完成块从未被调用,因此函数 createUserDocument 也不被调用。

这是实现我想要的最佳方式吗(也就是使用他们在注册时选择的图片的 imageURL 创建配置文件)? 为什么我的完成块没有被调用? (我没有在控制台中看到“成功”字样)。

感谢您的帮助!

亲切的问候,吉海斯

您需要调用传递给uploadImageToFirestore的完成处理程序闭包。

您可能还应该传递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