我一直在努力实现这一目标有一段时间了,不能让它的工作。
I've been trying to achieve this for some time and cannot get it to work.
首先让我告诉一个简单的示例code:
First let me show a simple sample code:
override func viewDidLoad() { super.viewDidLoad() methodOne("some url bring") } func methodOne(urlString1: String) { let targetURL = NSURL(string: urlString1) let task = NSURLSession.sharedSession().dataTaskWithURL(targetURL!) {(data, response, error) in // DO STUFF j = some value print("Inside Async1") for k in j...someArray.count - 1 { print("k = \(k)") print("Calling Async2") self.methodTwo("some url string") } } task.resume() } func methodTwo(urlString2: String) { let targetURL = NSURL(string: urlString2) let task = NSURLSession.sharedSession().dataTaskWithURL(targetURL!) {(data, response, error) in // DO STUFF print("inside Async2") } task.resume() }什么我基本上做的是我在我的 methodOne ,并在该函数内,我把我的 methodTwo 它执行另一个异步请求。
What I'm basically doing is I perform an asynchronous request within my methodOne, and within that function, I call my methodTwo which performs another asynchronous request.
我遇到的问题是,当一个名为 methodTwo ,它永远不会进入异步会话。然而,它的确,在 methodTwo ,输入异步会话,但只有一次 K = someArray.count - 1 。它基本上排队,直到最后一刻,这是不希望我来实现的。
The problem I'm having is that when methodTwo is called, it never enters the asynchronous session. It does however, within methodTwo, enter the asynchronous session, but only once k = someArray.count - 1. It's basically queuing up until the very end, which is not what I'd like to achieve.
下面是一个输出示例:
Inside Async1 k = 0 Calling Async2 Inside Async1 k = 0 Calling Async2 k = 1 Calling Async2 Inside Async1 k = 0 Calling Async2 k = 1 Calling Async2 k = 2 Calling Async2 Inside Async1 ..... Inside Async1 k = 0 Calling Async2 k = 1 Calling Async2 k = 2 Calling Async2 k = 3 Calling Async2 k = 4 Inside Async2在换句话说,我想有从异步请求 methodTwo 完成从 methodOne 完成。
In other words, I'd like to have the async request from methodTwo complete on each iteration before the async request from methodOne completes.
下面是我的目标是什么样的输出:
Here's a sample output of what my goal is:
Inside Async1 k = 0 Calling Async2 Inside Async2 Inside Async1 k = 1 Calling Async2 Inside Async2 Inside Async1 ...我发现这里类似的东西:Wait直到第一个异步功能完成后执行第二异步功能
不过,我不能让这与建议和解决方案的工作。
However, I couldn't get this to work with the suggestions and solutions.
有人能指出我在正确的方向?
Can someone point me in the right direction?
感谢
推荐答案这样做的一个办法是改变 methodTwo()接受回调作为参数,然后你可以使用一个信号:
One way to do this is to change methodTwo() to accept a callback as an argument, then you can use a semaphore:
func methodOne(urlString1: String) { let targetURL = NSURL(string: urlString1) let task = NSURLSession.sharedSession().dataTaskWithURL(targetURL!) { data, response, error in let queue = dispatch_queue_create("org.myorg.myqueue", nil) dispatch_async(queue) { // DO STUFF j = some value print("Inside Async1") for k in j...someArray.count - 1 { print("k = \(k)") print("Calling Async2") dispatch_semaphore_t sem = dispatch_semaphore_create(0); self.methodTwo("some url string") { dispatch_semaphore_signal(sem); } dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); } } } task.resume() } func methodTwo(urlString2: String, callback: (() -> ())) { let targetURL = NSURL(string: urlString2) let task = NSURLSession.sharedSession().dataTaskWithURL(targetURL!) { data, response, error in // DO STUFF print("inside Async2") callback() } task.resume() }请注意,为了不阻断methodOne的任务回调的委托队列,则建立自己的队列,你可以随意阻止。
Note that to not block the delegate queue of methodOne's task callback, the example creates its own queue that you can block at will.
更多推荐
如何使第一斯威夫特完成外异步请求之前内部异步请求完成?
发布评论