我有一个在func application(application:didFinishLaunchingWithOptions launchOptions:)中执行功能的网络任务。默认情况下,rootViewController是一个UITabBarController。我想同步我的品牌名单,从服务器下载的应用程序启动。我的代码如下:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.tabBarController = self.window?.rootViewController as! UITabBarController
.......
.......
.......
APICaller.getBrandsAndOutletList(withAuthToken: "87087fa228dee4fbbacada66683eb6fa94d4d8968dbc8121d275afe75a79e4b6d",
success: { (result) in
let rCode = result["rcode"] as! String
//If user revoked or access revoked for the user
guard rCode == "200" else {
let updateAppVC = UpdateAppViewController(nibName: "UpdateAppViewController", bundle: NSBundle.mainBundle())
if rCode == "401" {
let userStatus = result["status"] as! String
print(userStatus)
updateAppVC.message = userStatus
updateAppVC.buttonTitle = "Re-login"
self.window?.rootViewController = updateAppVC
//POINT-1
return
}else {
updateAppVC.message = "Some error"
updateAppVC.buttonTitle = "Retry"
self.window?.rootViewController = updateAppVC
//POINT-2
return
}
}
let brands = result["brands"] as! [[String:AnyObject]]
print(brands)
//POINT-3
}) { (errorMessage) in
print(errorMessage)
//POINT-4
}
return true //POINT-5
}现在,发生的是网络请求和列表下载是在后台进行的。执行return true并出现tabBar。然后,在请求完成后,调用success:或failure:块。
我想要实现的是,在完成请求之前,我不想使用return true。所以不想在第5点打电话给return true。相反,我想在1,2,3,4点调用return true,即当我的网络请求完成时。我能这样做吗?如果是,那怎么做?
发布于 2016-02-13 07:29:10
不能使用完成块为上述方法创建返回值。
从上面的代码听起来,您想要在启动时根据服务器提供的数据切换屏幕。当调用发生时,您不想显示Tab条控制器。您可以做的是拥有一个splash视图控制器(或一个屏幕简单活动指示器),并将其设置为根视图控制器,直到请求往返为止。
https://stackoverflow.com/questions/35377014
复制相似问题