Ios Firebase Swift 3使用NSURLSession将配置文件图像加载到TableView中

Ios Firebase Swift 3使用NSURLSession将配置文件图像加载到TableView中,ios,uitableview,firebase,swift3,nsurlsession,Ios,Uitableview,Firebase,Swift3,Nsurlsession,我按照教程加载图像URL并将其附加到数组中,以在每个用户的单元格的UITableView中显示用户配置文件图像,使用以下代码块: let url = NSURL(string: urlString) NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in //download hit an error so lets re

我按照教程加载图像URL并将其附加到数组中,以在每个用户的单元格的UITableView中显示用户配置文件图像,使用以下代码块:

let url = NSURL(string: urlString)
        NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in

        //download hit an error so lets return out
        if error != nil {
            print(error)
            return
        }

        dispatch_async(dispatch_get_main_queue(), {
            //"profilePics" is my array of UIImages made as a global var
            self.profileImages.append(UIImage[data!])
        })

    }).resume()
但由于某些原因,“NSURLSession”在我编程时不作为建议出现,它返回错误,因此我将所有内容从“NSURL…”更改为简单的“URL…”:


但每次我运行它时,数据都会得到一个nil,它永远不会附加到我的数组中。有人能解释一下我遗漏了什么吗?这是我第一次尝试Firebase。

我实际上发现了问题所在,我不想在其他人可能正在寻找相同问题的解决方案时删除帖子。事实证明,URLSession工作正常,所以找不到“NSURLSession”不是问题。然而,一旦我得到了工作的配置文件图像将只加载后重新加载我的uitableview。一旦我移动了tableview.reload(),它就会一直工作,下面是我的Swift 3工作代码:

//This method grabs all user data for each user & adds them to an 
//array to be displayed in a uitableview
 func getUsersData(){
    FIRDatabase.database().reference().child("users").observe(.childAdded, with: {(snapshot) in

        if let dictionary = snapshot.value as? [String:AnyObject]{

            print(snapshot.childrenCount)

            if (snapshot.childrenCount > 2) {

                self.users.append(dictionary["name"] as! String)

                if !(dictionary["subjects"]?.isEqual(""))!{

                    self.subjects.append(dictionary["subjects"] as! String)

                }

                else{

                    self.subjects.append("N/A")

                }

                if !(dictionary["location"]?.isEqual(nil))!{

                    self.states.append(dictionary["location"] as! String)

                }

                else{

                    self.states.append("N/A")

                }

                self.ratings.append("4.1")
                if let profileImageURL = dictionary["userPhoto"] {

                    let url = URL(string: profileImageURL as! String)
                    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in

                        if error != nil{

                            print(error!)
                            return

                        }

                        DispatchQueue.main.async {

                            self.profilePics.append(UIImage(data: data!))
                            print("Image appended")

                        }

                        // This line is required to reload with new data in tableview array data from db

                        DispatchQueue.main.async{

                            self.browseUITableView.reloadData()

                        }

                    }).resume()

                }
                // grabbing images from db end
            }
        }
    })
}

你为什么要向nsarray添加数据?这很糟糕practice@kirtimali你有什么建议?看看这个
//This method grabs all user data for each user & adds them to an 
//array to be displayed in a uitableview
 func getUsersData(){
    FIRDatabase.database().reference().child("users").observe(.childAdded, with: {(snapshot) in

        if let dictionary = snapshot.value as? [String:AnyObject]{

            print(snapshot.childrenCount)

            if (snapshot.childrenCount > 2) {

                self.users.append(dictionary["name"] as! String)

                if !(dictionary["subjects"]?.isEqual(""))!{

                    self.subjects.append(dictionary["subjects"] as! String)

                }

                else{

                    self.subjects.append("N/A")

                }

                if !(dictionary["location"]?.isEqual(nil))!{

                    self.states.append(dictionary["location"] as! String)

                }

                else{

                    self.states.append("N/A")

                }

                self.ratings.append("4.1")
                if let profileImageURL = dictionary["userPhoto"] {

                    let url = URL(string: profileImageURL as! String)
                    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in

                        if error != nil{

                            print(error!)
                            return

                        }

                        DispatchQueue.main.async {

                            self.profilePics.append(UIImage(data: data!))
                            print("Image appended")

                        }

                        // This line is required to reload with new data in tableview array data from db

                        DispatchQueue.main.async{

                            self.browseUITableView.reloadData()

                        }

                    }).resume()

                }
                // grabbing images from db end
            }
        }
    })
}