从 didReceiveRemoteNotification 调用的第一个 HTTP 请求总是失败
First HTTP Request called from didReceiveRemoteNotification always fails
我收到了一个简单的远程通知并像这样处理它:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let notificationObject = JSON(userInfo)
if notificationObject["type"] == "friendRequest" {
let tab = self.window?.rootViewController as! UITabBarController
tab.selectedIndex = 1
}
}
打开时,推送通知会显示一个 ViewController,它管理子类 UITableView
和 UITableViewCell
。
UITableViewCell
为每个单元格向 Google API 进行网络调用,如下所示:
let url = "https://www.googleapis.com/books/v1/volumes?q=\(isbn)&fields=items(volumeInfo(imageLinks(thumbnail),industryIdentifiers))&key=\(GOOGLE_BOOKS_API_KEY)"
Alamofire.request(.GET, url).response { response in
print(response)
除第一个单元格外,每个单元格的调用都成功,这给了我:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost.
启动时是否有任何 iOS limitations/delays/interference 网络调用?
我注意到有时你需要在执行某些失败的事情之前稍微延迟一下,以防你立即执行它们。
在调用方法之前,开始构建UITableView
和相应的单元格,将方法调用包装到dispatch_after
块中,延迟0.1秒,这应该足够了。
这应该可以解决第一个单元格的问题。如果这有帮助,您可以尝试延迟,尝试使其更短。
我收到了一个简单的远程通知并像这样处理它:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let notificationObject = JSON(userInfo)
if notificationObject["type"] == "friendRequest" {
let tab = self.window?.rootViewController as! UITabBarController
tab.selectedIndex = 1
}
}
打开时,推送通知会显示一个 ViewController,它管理子类 UITableView
和 UITableViewCell
。
UITableViewCell
为每个单元格向 Google API 进行网络调用,如下所示:
let url = "https://www.googleapis.com/books/v1/volumes?q=\(isbn)&fields=items(volumeInfo(imageLinks(thumbnail),industryIdentifiers))&key=\(GOOGLE_BOOKS_API_KEY)"
Alamofire.request(.GET, url).response { response in
print(response)
除第一个单元格外,每个单元格的调用都成功,这给了我:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost.
启动时是否有任何 iOS limitations/delays/interference 网络调用?
我注意到有时你需要在执行某些失败的事情之前稍微延迟一下,以防你立即执行它们。
在调用方法之前,开始构建UITableView
和相应的单元格,将方法调用包装到dispatch_after
块中,延迟0.1秒,这应该足够了。
这应该可以解决第一个单元格的问题。如果这有帮助,您可以尝试延迟,尝试使其更短。