代码之家  ›  专栏  ›  技术社区  ›  LizG

不确定在代码中的何处放置storageref.downloadurl

  •  0
  • LizG  · 技术社区  · 6 年前

    在将firebase更新到版本4并更正所有200个错误之后,我只剩下两个警告,这会导致我的应用程序崩溃。我查找了这个错误并尝试了这个解决方案,但没有成功:

    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

    0 回复  |  直到 6 年前
        1
  •  1
  •   Leo Dabus    6 年前

    您需要使用原始存储引用对象 imageRef 以获取下载URL。(请通过代码查看评论):

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