我有一个包含对象的类,为了使用该对象的属性,NSURLSession必须完成其异步数据请求。如何通过对象创建回调以了解该功能何时完成,我可以调用这些属性。
答案 0 :(得分:0)
你没有给出代码的例子,所以我将在摘要中写。您需要在NSURLSession
对象的完成内调用与您的对象一起使用的方法。例如,它可能如下所示:
// This is your object
struct SomeData {
var someValue: Int = 0
}
// This is class that use it
class Foo {
var someData: SomeData
init() {
someData = SomeData()
requestData()
}
// This is your function that need to wait for the request
func doActionWithData() {
print(someData.someValue)
}
// This is request
func requestData() {
// Make request with your params
let request = NSMutableURLRequest(...)
...
// For example you do it like this
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) in
// here you have data for your object you can get it from
// response and then call function to work with it
self.someData.someValue = ...
self.doActionWithData()
})
}
}