存在的快照返回null

时间:2017-03-17 12:57:18

标签: ios firebase firebase-realtime-database

遇到同样问题的问题 - 提取Firebase数据以填充用户个人资料。

我已经打印到日志以查看快照是否正在提取信息,但是它也说明了并且没有从Firebase数据库中提取任何信息。

Code : 
import UIKit
import Firebase
import SDWebImage

class UserViewController: UIViewController {

    @IBOutlet weak var userImage: UIImageView!
    @IBOutlet weak var userName: UILabel!

    var databaseRef: FIRDatabaseReference!
    var storageRef: FIRStorageReference!




    override func viewDidLoad() {
        super.viewDidLoad()

        databaseRef = FIRDatabase.database().reference()
        storageRef = FIRStorage.storage().reference()



        loadProfileData()
    }



    func loadProfileData(){
        FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid).observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot)
            if snapshot.exists(){
                let values = snapshot.value as! NSDictionary
                if let profileImageURL = values["urlToImage"] as? String {
                    self.userImage.sd_setImage(with: URL(string: profileImageURL))
                }
                self.userName.text = values["full name"] as? String

            }

        })
    }

记录:

2017-03-17 13:49:51.366564 avenir-design-app[14469:925571] [MC] Reading from private effective user settings.
2017-03-17 13:50:05.885290 avenir-design-app[14469:925211] [Common] _BSMachError: port 690f; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
2017-03-17 13:50:05.885899 avenir-design-app[14469:925211] [Common] _BSMachError: port 690f; (os/kern) invalid name (0xf) "Unable to deallocate send right"
Snap (XH1ju3X9QzfqZJFC4XP98IW4nn32) <null>

数据库:

{
  "user" : {
    "XH1ju3X9QzfqZJFC4XP98IW4nn32" : {
      "full name" : "Bob Jones",
      "uid" : "XH1ju3X9QzfqZJFC4XP98IW4nn32",
      "urlToImage" : "https://firebasestorage.googleapis.com/v0/b/avenir-design.appspot.com/o/users%2FXH1ju3X9QzfqZJFC4XP98IW4nn32.jpg?alt=media&token=4b92e6ac-6a4b-4bc8-825e-b674cc4372a4"
    },

1 个答案:

答案 0 :(得分:0)

我必须投入一本字典,然后确保我的孩子参考指向正确的孩子[我把用户,但它真的是用户]。但最重要的部分是投入字典。

这是更新后的代码:

import UIKit
import Firebase
import SDWebImage

class UserViewController: UIViewController {

@IBOutlet weak var userImage: UIImageView!
@IBOutlet weak var userName: UILabel!

var databaseRef: FIRDatabaseReference!
var storageRef: FIRStorageReference!

//let user = [User]()


override func viewDidLoad() {
    super.viewDidLoad()

    databaseRef = FIRDatabase.database().reference()
    storageRef = FIRStorage.storage().reference()

    //loadProfile()
    setupProfile()


    //loadProfileData()
}

func setupProfile(){
    if FIRAuth.auth()?.currentUser?.uid == nil {
        //logout()
    } else {
        let uid = FIRAuth.auth()?.currentUser?.uid
        databaseRef.child("user").child(uid!).observeSingleEvent(of: .value, with: {(snapshot) in
            if let dict = snapshot.value as? [String : AnyObject] {
                print(dict)
                self.userName.text = dict["full name"] as? String
                if let profileImage = dict["urlToImage"] as? String {
                    let url = URL(string: profileImage)
                    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
                        if error != nil {
                            print(error!)
                            return
                        }
                        DispatchQueue.main.async {
                            self.userImage?.image = UIImage(data: data!)
                        }

                    }).resume()
                }
            }

        })
    }
}