1
我使用NSURLConnection
下載存檔500MB。我想下載它在後臺線程,所以我寫了:NSURLConnection and beginbackgroundTaskWithExpirationHandler
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
self.theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
// Cancel the connection
[self.theConnection cancel];
}];
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID];
}
所以,如果我做這件事是主線程將其下載並解除封存,並且運行良好,但如果我開始下載,然後按Home
按鈕,所以它開始工作在後臺線程下載70% - 80%,並凍結。方法- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
不要打電話。
如何在後臺線程上下載大文件?
編輯1
我發現它叫做
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self.theConnection cancel];
}];
所以連接完成,但我不另一種方法叫endBackgroundTask
。
'NSURLSession'僅適用於iOS 7.0,但我需要支持iOS 6 – user2545330
我認爲這個問題出現在'App States and Multitasking'中。如何使用'apple developer'上可用的方法 - (void)applicationDidEnterBackground:(UIApplication *)application' – user2545330
>>後臺線程:你是說你按home和app運行後臺線程?如果是,我認爲你的應用程序將在10分鐘內死亡(https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html)>>如何下載大文件在後臺線程? :目前,在iOS 6中,據我所知,你不能 – nmh