gpt4 book ai didi

ios - iOS 中另一个请求中的 HTTP 请求

转载 作者:行者123 更新时间:2023-11-30 14:16:37 26 4
gpt4 key购买 nike

我需要在另一个请求中执行 HTTP 请求,据我所知,这需要同步。
在本例中,我获取“帖子”数组并获取有关它们的信息。
这是代码,可以帮助您更好地了解情况:

// getPosts()
...
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse?, error: NSError?) -> Void in
if response != nil {
let dataDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &err) as [NSDictionary]
for postDic: NSDictionary in dataDictionary {
let post = Post(postId: postDic["post_id"] as String)

post.likes = postDic["likes"] as String
post.views = postDic["views"] as String

self.getCommentCountForPostID(post.postId, completion: { (count) -> Void in
post.comments = count
})

...
self.posts.addObject(post)
}
// reloading table view etc
}
})
task.resume()

这是函数getCommentCountForPostID:

func getCommentCountForPostID(postID: String, completion: ((count : String?) -> ())) {
...
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse?, error: NSError?) -> Void in
if response != nil {
let resultCount: NSString! = NSString(bytes: data.bytes, length: data.length, encoding: NSASCIIStringEncoding)
print(resultCount)

completion(count: resultCount)
}

})

task.resume()
}

问题是 getCommentCountForPostID 函数在 for 循环向前运行时异步调度。我需要该函数首先完成评论请求,然后才继续执行 getPosts()。我尝试过使用 dispatch_sync() 将不同部分包装在代码中的多种组合,但没有成功。
我怎样才能实现这个目标?非常感谢!

最佳答案

使用递归来达到您的目的。

加载第一篇文章 -> 处理它 -> 加载下一篇文章 -> 当所有文章加载完毕后完成。

我创建了名为“loopAndLoad”的辅助方法,它将您的数据数组作为参数,以及要处理的当前项目的索引

let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse?, error: NSError?) -> Void in
if response != nil {
let dataDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &err) as [NSDictionary]
self.loopAndLoad(0, dataDictionary);
}
})
task.resume()



func loopAndLoad( index: Int, dataArray: Array<NSDictionary>)
{
let postDic = dataArray[index]
let post = Post(postId: postDic["post_id"] as String)

post.likes = postDic["likes"] as String
post.views = postDic["views"] as String

self.getCommentCountForPostID(post.postId, completion: { (count) -> Void in
post.comments = count
self.posts.addObject(post)
if (++index < dataArray.count)
{
self.loopAndLoad(index, dataArray)
}
else
{
/// end of loop
}
})
}


func getCommentCountForPostID(postID: String, completion: ((count : String?) -> ())) {
...
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse?, error: NSError?) -> Void in
if response != nil {
let resultCount: NSString! = NSString(bytes: data.bytes, length: data.length, encoding: NSASCIIStringEncoding)
print(resultCount)

completion(count: resultCount)
}

})

task.resume()
}

关于ios - iOS 中另一个请求中的 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com