在iOS上,您能否发出同步网络请求(但不在主线程上),并且仍然获得进度回调(在单独的非主线程上)? [英] On iOS, can you make a synchronous network request (but not on the main thread) and still get progress callbacks (on a separate, non-main thread)?

查看:217
本文介绍了在iOS上,您能否发出同步网络请求(但不在主线程上),并且仍然获得进度回调(在单独的非主线程上)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS上,您可以发出同步网络请求(在主线程之外)并获取进度回调(在单独的非主线程上)吗?

On iOS, can you make a synchronous network request (off the main thread) and get progress callbacks (on a separate, non-main thread)?

我有一个串行(每次一次操作)后台队列,该队列运行所有耗时的工作,这些工作现在不需要完成正确.我确实想显示下载作业的进度.看起来您无法实例化NSURLConnection并配置委托,启动同步连接,然后获取进度回调.

I have have a serial (one-operation-at-a-time) background queue that runs all of time-consuming jobs that don't need to finish right now. I do want to show progress for the download jobs though. It doesn't look like you can instantiate an NSURLConnection and configure a delegate, start synchronous connection, and then get progress callbacks.

是否有一种方法可以在该后台队列上发出同步请求(同步,因为它后面的作业要等到完成才开始),并且仍然获得可以发送来更新进度条的setProgress:回调? (回调必须位于不同的队列线程上,因为在请求完成之前,我的串行队列的线程一直处于阻塞状态.)

Is there a way to make a synchronous request on that background queue (synchronous in that the job behind it doesn't start until its done), and still get setProgress: callbacks which could be sent to update a progressbar? (Callbacks would have to be on a different queue thread, since my serial queue's thread is blocked until the request is finished.)

Apple的文档NSURLConnection 说,同步请求实际上是在幕后异步之上构建的.我需要重新实现吗?我需要一种方法来阻止线程,直到请求完成/失败.到目前为止,我拥有的最好的线索是NSOperationQueuewaitUntilFinished方法,但是我不想启动异步并不断地对同步方法进行轮询.

Apple's docs for NSURLConnection say that the synchronous request is actually built on top of the asynchronous behind the scenes. Do I have to re-implement that? I need a way to block a thread until the request finishes/fails. The best leads I have so far are NSOperationQueue's waitUntilFinished method, but I don't want to start async and continually poll on the synchronous method.

NSURLConnection讨论

在类提供的异步加载代码的基础上构建了一个同步加载.异步加载系统在专门为此加载请求生成的线程上执行URL加载时,调用线程被阻止.调用线程中不需要特殊的线程或运行循环配置即可执行同步加载.

A synchronous load is built on top of the asynchronous loading code made available by the class. The calling thread is blocked while the asynchronous loading system performs the URL load on a thread spawned specifically for this load request. No special threading or run loop configuration is necessary in the calling thread in order to perform a synchronous load.

推荐答案

在异步请求之上重新实现同步请求并不难.您只需要手动旋转线程的运行循环,直到看到请求已完成(或失败)即可.这是一个粗略的示例:

Reimplementing a synchronous request on top of an asynchronous request is not that hard. You just need to manually spin the thread's run loop until you see that the request has finished (or failed). Here's a rough example:

NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:queryURL];
// Start the download and wait until it finishes
self.downloadFinished = NO;
self.downloader = [NSURLConnection connectionWithRequest:downloadRequest delegate:self];

while (!self.isDownloadFinished)
 {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
 }

以下是相关的NSURLConnectionDelegate方法:

Here are the relevant NSURLConnectionDelegate methods:

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection;
 {
    self.downloadFinished = YES;
 }

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
 {
    NSLog(@"An error occurred: %@", error);
    self.receivedData = nil;
    self.downloadFinished = YES;
 }

这篇关于在iOS上,您能否发出同步网络请求(但不在主线程上),并且仍然获得进度回调(在单独的非主线程上)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆