如何使第一斯威夫特完成外异步请求之前内部异步请求完成?

编程入门 行业动态 更新时间:2024-10-11 15:22:05
本文介绍了如何使第一斯威夫特完成外异步请求之前内部异步请求完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在努力实现这一目标有一段时间了,不能让它的工作。

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.

更多推荐

如何使第一斯威夫特完成外异步请求之前内部异步请求完成?

本文发布于:2023-11-26 02:04:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632213.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斯威夫特

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!