单独线程上的异步NSURLConnection无法调用委托方法 [英] Asynchronous NSURLConnection on separate thread fails to call delegate methods

查看:116
本文介绍了单独线程上的异步NSURLConnection无法调用委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个单独的线程上运行NSURLConnection(我知道它是异步的并且在主线程上运行时有效),但即使我将父线程作为委托传递,它也不会进行委托调用。有谁知道怎么做?

I am running a NSURLConnection on a separate thread (I am aware that it is asynchronous and works when running on the main thread), but it is not making delegate calls even when I pass the parent thread as the delegate. Does anyone know how to do this?

代码:

-(void)startConnectionWithUrlPath:(NSString*)URLpath {

//initiates the download connection - setup
NSURL *myURL = [[NSURL alloc] initWithString:URLpath];

myURLRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[myURL release];


//initiates the download connection on a seperate thread
[NSThread detachNewThreadSelector:@selector(startDownloading:) toTarget:self withObject:self];

}


-(void)startDownloading:(id)parentThread {

 NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];

 [NSURLConnection connectionWithRequest:myURLRequest delegate:parentThread];
 //The delegate methods are setup in the rest of the class but they are never getting called...

 [pool drain];
}

编辑 *

EDIT*

我需要在一个单独的线程上运行NSURLConnection是因为我在我的iPhone应用程序中下载了一些东西,当用户锁定屏幕时下载取消(它继续正常如果用户只需按下主页按钮,应用程序就会进入后台)。我理解这是因为我在主线程上异步运行连接而不是单独的连接。

The reason I need to run NSURLConnection on a separate thread is because I am downloading something in my iPhone app and the download cancels when the user locks the screen (it continues fine if the user simply presses the home button and the app goes into the background). I understand this is due to my running the connection asynchronously on the main thread and not a separate one.

我在启动NSURLConnection时也试过这段代码(不在单独的线程中):

I have also tried this code (NOT in a separate thread) when initiating the NSURLConnection:

  NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:myURLRequest delegate:self startImmediately:NO];
   [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
   [connection start];
   [connection release];

但是我在屏幕锁定时取消下载时遇到了同样的问题。

But it I have the same problem with this regarding the download being cancelled on screen lock.

* 更新

要添加到Thomas的回答(请注意James Webster的回答关于退出线程也是正确的)Apple文档解释:
暂停状态 - 应用程序在后台但没有执行代码。系统会自动将应用程序移动到此状态,并且在执行之前不会通知它们暂停时,应用程序仍保留在内存中,但不会执行任何代码。

To add to Thomas' answer below (Please note that James Webster's answer is also correct regarding the exiting of a thread) the Apple docs explain: "Suspended state - The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code."

因为当用户锁定屏幕时,应用程序将进入后台状态并且直接进入暂停状态,所有执行都停止杀死任何下载并且没有警告即将发生这种情况......可能有通知告诉我用户已锁定屏幕但我还没有发现了一个。

Since when the screen is locked by the user the app is put into the background state and than right away into the suspended state, all execution is stopped killing any downloads and no warning that this is about to happen is given... there may be a notification which tells me that the user has locked the screen but I haven't found one yet.

因此我暂停(保存一定当应用程序进入后台时,所有下载信息并取消NSURLConnection),当它再次激活时,使用HTTP Range标头恢复它。
这是一个可行但不理想的解决方法,因为下载不会在背景中发生,从而对用户体验产生负面影响......糟糕。

I therefore pause (save certain information and cancel the NSURLConnection) all downloads when the app goes into the background and resume it with the HTTP Range header when it gets active again. This is a workaround which is ok but not ideal since the download is not occurring in the background which affects the user experience negatively... bummer.

推荐答案

由于您的NSURLConnection是异步的,因此会立即到达-startDownloading方法的结尾,并退出该线程。

Since your NSURLConnection is asynchronous, the end of your -startDownloading method is reached immediately, and the thread exits.

您确实应该在主runloop上安排连接(或使用GCD)。

You should indeed schedule your connection on the main runloop (or use GCD).

设备锁定是另一个问题。设备锁定后,您的应用程序将暂停以延长电池寿命。您可以要求额外的时间暂停时以完成下载。

The device lock is another issue. When the device is locked, your application is suspended to save battery life. You can probably ask for an extra amount of time when suspending in order to finish your download.

这篇关于单独线程上的异步NSURLConnection无法调用委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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