0

I was following a tutorial to load Image URL's and appending them to an array to display user profile images in a UITableView of cells for each user, using the following block of code:

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

But for some reason "NSURLSession" does not appear as a suggestion while I am programming and it returns errors, so I changed the everything from "NSURL..." to simply "URL..." :

                //Grabbing images from db
                print("starting get image block : ")
                    let url = URL(string: profileImageURL)
                    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
                        print("grabbing image: ")       //Test line for monitor
                        print(String(describing: data)) //Test line for monitor : returns "nil"
                        if error != nil{
                            print(error!)
                            return
                        }

                        DispatchQueue.main.async {
                            //"profilePics" is my array of UIImages made as a global var
                            self.profilePics.append(UIImage(data: data!))
                            print("Image appended")
                        }

                 }).resume()

                 // grabbing images from db end

But each time I run this I get a nil for data, and it is never appended to my array. Can someone explain what I am missing? This is my first attempt with Firebase.

1QuickQuestion
  • 729
  • 2
  • 9
  • 25

1 Answers1

0

I actually found what the problem was, and I didn't want to just delete the post in the event that someone else may be looking for a solution to the same problem. Turns out, URLSession works fine so not finding "NSURLSession" was not a problem. However, once I got that working the profile image would only load after reloading my uitableview. Once I move my tableview.reload() it worked consistently, and here is my working code for 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
            }
        }
    })
}
1QuickQuestion
  • 729
  • 2
  • 9
  • 25