代码之家  ›  专栏  ›  技术社区  ›  Lukas Bimba

Swift Firebase save downloadURL字符串

  •  -1
  • Lukas Bimba  · 技术社区  · 6 年前

    我更新了我的firebase代码,因为它们更改了“metaData!”!。下载URL()!。absoluteString”输入我将在下面发布的代码中。我的问题是我需要将downloadURL字符串与文章的其余信息一起保存。因为它是一个异步方法,所以我无法访问调用外部的字符串,当我尝试将“.setValue”放在调用中时,它就不起作用了。这是我之前的问题,但没有关于这个问题的答案: Swift Firebase metaData!.downloadURL()!.absoluteString

    let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
        let usersRef = Database.database().reference().child("Businesses")
        let databaseRef = Database.database().reference()
        let imageName = NSUUID().uuidString
        let photoRef = photosRef.child("\(uid)")
        let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
        var downloadURLSting = String()
    
        photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
            if let error = error {
                print("there was an error")
                print(error.localizedDescription)
                return
            } else {
                // store downloadURL
    
    
                storage.reference().downloadURL(completion: {(url, error) in
                    if error != nil {
                        print(error!.localizedDescription)
                        return
                    }
                    let downloadURL = url?.absoluteString
    
                })
    
                let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]
    
                // store downloadURL at database
                let databaseRef = Database.database().reference()
                let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
                path.setValue(values) { (error, ref) -> Void in
                    if error != nil {
                        print("error saving post in db")
                    } else {
                        // reset caption field
                        self.descriptionTextView.text = ""
                        // reset placeholder image
                        self.imageView.image = UIImage(named: "filterPlaceholder")
                        MBProgressHUD.hide(for: self.view, animated: true)
                        let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
                        self.present(viewConrolller, animated: true, completion: nil)
                    }
                }
            }
        }
    

    安全规则

    service firebase.storage {
    match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
    }
    }
    

    此代码仅在“downloadURL”返回nil时工作

    let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
        let usersRef = Database.database().reference().child("Businesses")
        let databaseRef = Database.database().reference()
        let imageName = NSUUID().uuidString
        let photoRef = photosRef.child("\(uid)")
        let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
        photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
            if let error = error {
                print("there was an error")
                print(error.localizedDescription)
                return
            } else {
                // store downloadURL
                photoRef.downloadURL(completion: {(url, error) in
                    if error != nil {
    
                        guard let downloadURL = url?.absoluteString else { return }
    
                        let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]
    
                        // store downloadURL at database
                        let databaseRef = Database.database().reference()
                        let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
                        path.setValue(values) { (error, ref) -> Void in
                            if error != nil {
                                print("error saving post in db")
                            } else {
                                // reset caption field
                                self.descriptionTextView.text = ""
                                // reset placeholder image
                                self.imageView.image = UIImage(named: "filterPlaceholder")
                                MBProgressHUD.hide(for: self.view, animated: true)
                                let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
                                self.present(viewConrolller, animated: true, completion: nil)
                            }
                        }
                    } else {
                        print(error!.localizedDescription)
                        print("error")
                        return
                    }
                })
            }
        }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Eric H    6 年前

    这可能是因为变量不在同一范围内。如果你试着这样做会怎么样?

    photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
        if let error = error {
            print("there was an error")
            print(error.localizedDescription)
            return
        } 
    
        storage.reference().downloadURL(completion: {(url, error) in
                if error != nil {
                    print(error!.localizedDescription)
                    return
                }
                let downloadURL = url?.absoluteString
    
                let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]
    
            let databaseRef = Database.database().reference()
            let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
            path.setValue(values) { (error, ref) -> Void in
                if error != nil {
                    print("error saving post in db")
                } else {
                    // reset caption field
                    self.descriptionTextView.text = ""
                    // reset placeholder image
                    self.imageView.image = UIImage(named: "filterPlaceholder")
                    MBProgressHUD.hide(for: self.view, animated: true)
                    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
                    self.present(viewController, animated: true, completion: nil)
                }
            }
    
        })        
    }