- 我的问题:
我正在尝试通过 Alamofire ,然后SubViewController加载其视图。编写代码后,我无法解决Alamofire的异步功能的问题。在Alamofire完成工作之前,始终将视图加载到SubViewController中。
I am trying to load data from Server, through Alamofire, before SubViewController Load its view. After writing the code, I failed to solve the problem of Async Feature of Alamofire. The view is always be loaded in the SubViewController before Alamofire finished its job.
- 部分代码:
ParentViewController:
ParentViewController:
通过 PrepareForSegue()进入SubViewController。
Leading the way to SubViewController through PrepareForSegue().
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "CellDetailSegue" { if let indexPaths = self.dateCollectionView.indexPathsForSelectedItems() { let subViewController = segue.destinationViewController as! SubViewConroller } }SubViewController:
SubViewController:
测试数据是否已由 print()在其 viewDidLoad()中加载并在 viewWillAppear()
Test whether the data has been loaded by print() in the its viewDidLoad() and load the data by dataRequest() in viewWillAppear()
class SubViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { var availablePeriods401 = [String]() var availablePeriods403 = [String]() var availablePeriods405 = [String]() override func viewDidLoad() { super.viewDidLoad() self.dataRequest(self.availablePeriods401) self.dataRequest(self.availablePeriods403) self.dataRequest(self.availablePeriods405) print(self.availablePeriods401.count) print(self.availablePeriods403.count) print(self.availablePeriods405.count) } func dataRequest(_ target: [String]) { Alamofire.request(.POST, "httpbin/get", parameters: ["foo": "bar"]).responseJSON { . . . target = Result } } }
- 问题描述:
- 我的问题:
- 嵌套网络通话,以便完成一个开始下一个。这种方法的缺点是它们将按顺序运行,如果添加更多,则必须继续嵌套。如果您仅进行2次调用,则这样的方法可能会行,但除此之外,它将变得越来越困难。
- 使用信号量等待直到从所有加载所有数据为止。远程通话。使用完成处理程序来发信号量。如果要使用此方法,则必须在后台线程上完成,因为使用信号量会阻塞该线程,并且您不希望在主线程上发生这种情况。
不能为SubViewController中的三个变量分配有效值视图加载后。
Three variables in the SubViewController can not be assigned the valid values after view was loaded.
三个输出的结果全部为0。
Three Outputs' results are all 0.
但是如果设置<$ c,我可以获得有效计数 dataRequest()中的$ c> print()。
But I can get valid count if I set print() in the dataRequest().
如何确保 Alamofire 完成
我应该在哪里放置Alamofire请求功能? viewWillApper()? viewDidApper()?
Where Shall I put the Alamofire Request Function? viewWillApper()? viewDidApper()?
我什至应该在ParentViewController的 PrepareForSegue()中完成请求工作吗? / code>?
Should I even finished requesting job in ParentViewController's PrepareForSegue() ?
请教我如何解决此问题。
Please teach me how to solve this problem.
非常感谢您的指导和时间。
A big appreciation for your guide and time.
Ethan Joe
Ethan Joe
推荐答案我注意到的第一件事是您正在执行3个异步请求,而不是一个。您可以使用完成处理程序,但是哪个?我认为您有2个选择。
The first thing I noticed is that you are doing 3 asynchronous requests, not one. You could use a completion handler but which one? I think you have 2 options.
这三个呼叫将同时发生。即使AlamoFire尚未完成,该函数也会返回。
These three calls will all happen simultaneously. And the functions will return even though AlamoFire has not completed.
self.dataRequest(self.availablePeriods401) self.dataRequest(self.availablePeriods403) self.dataRequest(self.availablePeriods405)这些将执行,无论AlamoFire是否已完成。
These will execute, whether AlamoFire has completed or not.
print(self.availablePeriods401.count) print(self.availablePeriods403.count) print(self.availablePeriods405.count)使用信号量看起来像这样:
Using semaphores would look something like this:
override func viewWillAppear(animated: Bool) { // maybe show a "Please Wait" dialog? loadMyData() { (success) in // hide the "Please Wait" dialog. // populate data on screen } } func loadMyData(completion: MyCompletionHandler) { // Do this in an operation queue so that we are not // blocking the main thread. let queue = NSOperationQueue() queue.addOperationWithBlock { let semaphore = dispatch_semaphore_create(0) Alamofire.request(.POST, "httpbin/get", parameters: ["foo": "bar1"]).responseJSON { // This block fires after the results come back // do something dispatch_semaphore_signal(semaphore); } Alamofire.request(.POST, "httpbin/get", parameters: ["foo": "bar2"]).responseJSON { // This block fires after the results come back // do something dispatch_semaphore_signal(semaphore); } Alamofire.request(.POST, "httpbin/get", parameters: ["foo": "bar3"]).responseJSON { // This block fires after the results come back // do something dispatch_semaphore_signal(semaphore); } dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) completion(true) } }Apple Docs-Grand Central Dispatch
如何使用信号量
我要为您解决的问题是,如果不是所有的网络电话都失败了,您将怎么办?
The question I have for you is what are you going to do if some, bit not all of the web calls fail?
更多推荐
Alamofire完成工作后如何加载视图?
发布评论