ios - 不确定在代码中放置 storageRef.downloadURL 的位置

标签 ios swift firebase-storage

将 Firebase 更新到版本 4 并更正所有 200 个错误后,我收到 2 条警告,导致我的应用程序崩溃。我查找了这个错误并尝试了解决方案但没有成功:

storageReference.downloadURLWithCompletion()

一定是我做错了:

func setUserInfo(_ user: User!, usersname: String, email: String, password: String, cell: String, data: Data!) {

    // Create Path for User Image
    let imagePath = "images/riders/\(user.uid)/Profile Pic/userPic.jpg"

    // Create image Reference
    let imageRef = rDataService.Instance.storageRef.child(imagePath)

    // Create Metadata for the image
    let metaData = StorageMetadata()
    metaData.contentType = "image/jpeg"

    // Save the user Image in the Firebase Storage File
    imageRef.putData(data as Data, metadata: metaData) { (metaData, error) in
        if error == nil {
            let changeRequest = user.createProfileChangeRequest()
            changeRequest.displayName = usersname
            changeRequest.photoURL = metaData?.downloadURL()
            changeRequest.commitChanges(completion: { (error) in

                if error == nil {
                    self.saveUser(user, usersname: usersname, email: email, password: password, cell: cell)

                } else {
                    print(error!.localizedDescription)
                }
            })

        } else {
            print(error!.localizedDescription)
        }
    }

}

错误发生在这一行:

changeRequest.photoURL = metaData?.downloadURL()

编辑

调整后,在这一行得到警告:

if let profilepicMetaData = profilepicMetaData {

error: Value 'profilepicMetaData' was defined but never used; consider  replacing with boolean test

应用仍然崩溃:

// Save the user profile Image in the Firebase Storage File
    imageRef.putData(data as Data, metadata: profilepicMetaData) { (profilepicMetaData, error) in
        if let profilepicMetaData = profilepicMetaData {
            imageRef.downloadURL(completion: { (url, error) in

                guard let url = url else {
                    if let error = error {
                        print(error)
                    }
                    return
                }
                let changeRequest = user.createProfileChangeRequest()
                changeRequest.displayName = usersname
                changeRequest.photoURL = url

                changeRequest.commitChanges(completion: { (error) in

                if error == nil {
                    self.saveUser(user, usersname: usersname, email: email, password: password, year: year, makeAndModel: makeAndModel, cell: cell, plateNo: plateNo)

                } else {
                    print(error!.localizedDescription)
                }
            })
        })

        } else {
            print(error!.localizedDescription)
        }
    }

崩溃!

crash screen shot

最佳答案

需要使用原始存储引用对象imageRef获取下载地址。 (请通过代码查看注释):

imageRef.putData(data, metadata: profilepicMetaData) {
    // use if let to unwrap the metadata returned to make sure the upload was successful. You can use an underscore to ignore the result
    if let _ = $0 {
        // start the async method downloadURL to fetch the url of the file uploaded
        imageRef.downloadURL {
            // unwrap the URL to make sure it is not nil
            guard let url = $0 else {
                // if the URL is nil unwrap the error, print it 

                if let error = $1 { 
                    // you can present an alert with the error localised description
                    print(error) 
                }
                return
            }
            // your createProfileChangeRequest code  needs to be run after the download url method completes. If you place it after the closure it will be run before the async method finishes.
            let changeRequest = user.createProfileChangeRequest()
            changeRequest.displayName = usersname
            changeRequest.photoURL = url
            changeRequest.commitChanges {
                // unwrap the error and print it
                if let error = $0 {
                    // again you might present an alert with the error
                    print(error)
                } else {
                    // user was updated successfully
                    self.saveUser(user, usersname: usersname, email: email, password: password, year: year, makeAndModel: makeAndModel, cell: cell, plateNo: plateNo)
                }
            }
        }
    } else if let error = $1 {
        // present an alert with the error
        print(error)
    }
}

关于ios - 不确定在代码中放置 storageRef.downloadURL 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54758190/

相关文章:

swift - 新的 Firebase 检索数据并放在 tableView 上 (Swift)

ios - pdfView 捏合缩小

ios - 在 iPad 应用程序和 iPhone 之间使用 iBeacon 和 Passbook 是否可以识别此人?

javascript - Firebase 安全规则来自特定 Web url 的存储

ios - 如何根据 Swift 中元素的 id 从结构数组中删除结构元素?

ios - 为 UITableView 中的每个部分添加特定数据 - Swift

android - Firebase 存储上传进度不准确

ios - UIPageViewController 检测平移手势

objective-c - NSURL连接优化

swift - 在 UICollectionView 中选择单个 UILabel