更新数组Swift 4

时间:2018-12-07 08:42:47

标签: ios swift firebase firebase-realtime-database

我正在尝试通过从Firebase数据库下载图像来更新图像阵列。但是由于某种原因,在函数内部更新数组之后,不会在viewdidload函数中对其进行更新。我是xcode的新手。有任何想法吗?

var images:[UIImage] = []
override func viewDidLoad() {
    super.viewDidLoad()
    ref = Database.database().reference()
    retrieveImage()
    print(self.images)
}

func retrieveImage(){
    let userID = Auth.auth().currentUser?.uid

    ref.child("Images").observeSingleEvent(of: .value, with: { (snapshot) in

        let userImage = snapshot.value as? NSDictionary
        let imageURLArray = userImage?.allKeys

        if userImage != nil{

            for index in 0...userImage!.count-1{
                let imageProfile = userImage![imageURLArray?[index]] as? NSDictionary
                let imageURL = imageProfile!["url"]
                let storageRef = Storage.storage().reference(forURL: imageURL as! String)
                storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
                    if let error = error {
                        print(error.localizedDescription)
                    } else {

                        let image = UIImage(data: data!)
                        self.images.append(image!)

                    }
                }

            }
        }
    }) { (error) in
        print(error.localizedDescription)
    }

}

2 个答案:

答案 0 :(得分:2)

因为observeSingleEvent异步运行,所以在observeSingleEvent完成之前调用了打印功能。 您可以像这样修复它,使用闭包

var images:[UIImage] = []
override func viewDidLoad() {
    super.viewDidLoad()
    ref = Database.database().reference()
    retrieveImage {
        print(self.images)
    }
}
func retrieveImage(_ completion: () -> Void){
    let userID = Auth.auth().currentUser?.uid

    ref.child("Images").observeSingleEvent(of: .value, with: { (snapshot) in

    let userImage = snapshot.value as? NSDictionary
    let imageURLArray = userImage?.allKeys

    if userImage != nil{

        for index in 0...userImage!.count-1{
            let imageProfile = userImage![imageURLArray?[index]] as? NSDictionary
            let imageURL = imageProfile!["url"]
            let storageRef = Storage.storage().reference(forURL: imageURL as! String)
            storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
                if let error = error {
                    print(error.localizedDescription)
                } else {

                    let image = UIImage(data: data!)
                    self.images.append(image!)

                }
            }

        }
    }
    completion()
}) { (error) in
    print(error.localizedDescription)
    completion()
}
}

答案 1 :(得分:0)

您可以这样使用

var images:[UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
retrieveImage {
    print(self.images)
}
}

func retrieveImage(_ completion: () -> Void){
let userID = Auth.auth().currentUser?.uid

ref.child("Images").observeSingleEvent(of: .value, with: { (snapshot) in

    let userImage = snapshot.value as? NSDictionary
    let imageURLArray = userImage?.allKeys

    if userImage != nil {
        for index in 0...userImage!.count-1 {
            let imageProfile = userImage![imageURLArray?[index]] as? NSDictionary
            let imageURL = imageProfile!["url"]
            let storageRef = Storage.storage().reference(forURL: imageURL as! String)
            storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
                if let error = error {
                    completion()
                    print(error.localizedDescription)
                } else {
                    let image = UIImage(data: data!)
                    self.images.append(image!)
                    if index == userImage!.count-1 {
                        completion()
                    }
                }
            }

        }
    } else {
        completion()
    }
}) { (error) in
    print(error.localizedDescription)
    completion()
}

}