Abdul Waheed
Abdul Waheed

Reputation: 894

Array gets nil GCD

When I run this code it works fine but when I run it in dispatch_async it gets error in tableViews function numberOfRowsInSection

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
    self.data = self.dataOfJson("http://192.168.1.100/practice/studentCourseSelection.php?ID=\(NSUserDefaults.standardUserDefaults().objectForKey("currentUser")!)")
    self.RefreshTableView()
}

func RefreshTableView()
{
    dispatch_async(dispatch_get_main_queue()){
        self.tableView.reloadData()
    }
}
 func dataOfJson(url:String) -> NSArray
{
    var jsonArray : NSMutableArray = []

    let data = NSData(contentsOfURL: NSURL(string : url)!)
    jsonArray = try! NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSMutableArray



     return jsonArray
}

enter image description here

Upvotes: 1

Views: 85

Answers (1)

Code
Code

Reputation: 6251

You should modify self.data on the main thread.

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
    let data = self.dataOfJson("http://192.168.1.100/practice/studentCourseSelection.php?ID=\(NSUserDefaults.standardUserDefaults().objectForKey("currentUser")!)")
    self.RefreshTableView(data)    
}

func RefreshTableView(data: ??) {
    dispatch_async(dispatch_get_main_queue()){
        self.data = data
        self.tableView.reloadData()
    }
}

Upvotes: 2

Related Questions