I'm trying to receive data from the network with dispatch_async in Swift. Here is a piece of code which runs new thread:
override func viewWillAppear(animated: Bool) {
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue, {
dispatch_async(queue, {
self.loadData()
})
})
}
And here is my loadData():
func loadData() {
var feedItems = [Item]()
let stringUrl = BASE_URL + "newsfeed.get?filters=photo,wall_photo&count=100&max_photos=10&source_ids=friends,following&v=5.28&access_token=" + accessToken
let url = NSURL(string: stringUrl)
let urlRequest = NSURLRequest(URL: url!)
var response: NSURLResponse?
var error: NSError?
println("Loading data")
let data = NSURLConnection.sendSynchronousRequest(urlRequest, returningResponse: &response, error: &error)
if data != nil && error == nil {
...
}
But I'm always getting code=exc_i386_invop subcode=0x0 in the last if statement. If I remove data!= nil, everything seems to be ok but I'm getting the same error in the next if statement while checking another error for nil. What's wrong?
Finally figured out. It was something wrong with the project. I created a new project and copied source code there. Now it works perfect.