Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How reload data table view from other class ?

I have one ViewController with tableView. Now i create some simple class for example: CLass1 and in this class is working dispatch_get_global_queue waiting for data from server. How can reloadData in table view from this class. I can not use NSNotification reason is thread can't modificative other view. So i need do in UI from this class Class1.

1 Answer

I normally create a function with a completion block parameter that will be called after the background request is ran.

// request class
class func runRequest(completion: () -> ()) {

    // request = NSURLRequest(URL : url)

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.currentQueue(), completionHandler: { (response, data, error) -> Void in

        dispatch_async(dispatch_get_main_queue(), { () -> Void in

            completion()

        })

    })

}

// view controller class
override func viewDidLoad() {

    Request.runRequest(completion: { () in 

        tableView.reloadData()

    })

}