任何人都知道如何从主线程中停止后台线程的运行循环?
我有一个类,它具有从服务器下载文件的功能。从主线程我在后台线程中调用函数sendHttpRequest从服务器下载文件。
[self performSelectorInBackground:@selector(sendHttpRequest:) withObject:file];
我使用过CFRunLoopRun();接收回调并避免使用CFRunLoopStop(CFRunLoopGetCurrent())退出线程并停止运行循环;下载完成后。
但我需要在下载数据时停止下载,如何停止运行循环?
任何帮助......
这是我的代码:
#import "HTTPHandler.h"
@implementation HTTPHandler
@synthesize fileName;
NSURLConnection *urlConnection;
NSMutableData *receivedData;
NSInteger receivedStatusCode;
- (id)initHttpHandler
{
self = [super init];
httpEvent = nil;
return self;
}
- (void)downloadFile:(NSString *)file
{
NSLog(@"Download file : %@", file);
fileName = file;
NSString *url = [NSString stringWithFormat:@"http://temporaryServerUrl&fileName=%@", fileName];
NSLog(@"URL : %@", url);
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
// create the connection with the request and start loading the data
[request setHTTPMethod:@"GET"];
[request setValue:[NSString stringWithFormat:@"text/plain,application/xml"] forHTTPHeaderField:@"Accept"];
urlConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (urlConnection)
{
// Create the NSMutableData to hold the received data.
receivedData = [[NSMutableData data] retain];
}
else
{
NSLog(@"HTTP connection failed to download file.");
}
NSLog(@"Run loop started");
CFRunLoopRun(); // Avoid thread exiting
NSLog(@"Run loop stoped");
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace (NSURLProtectionSpace *)protectionSpace
{
if ([NSURLAuthenticationMethodHTTPDigest compare:[protectionSpace authenticationMethod]] == NSOrderedSame)
{
return YES;
}
else
{
NSLog(@"Warning: Unsupported HTTP authentication method.");
}
return NO;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge (NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0)
{
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:@"admin" password:@"1234" persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}
else
{
NSLog(@"Going to cancel the authentication challenge.");
[[challenge sender] cancelAuthenticationChallenge:challenge];
// inform the user that the user name and password in the preferences are incorrect
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
[receivedData setLength:0];
receivedStatusCode = httpResponse.statusCode;
if (httpResponse.statusCode == 200)
{
NSLog(@"200 ok received");
}
else
{
// We can also add condition that status code >= 400 for failure.
NSLog(@"%d status code is received.", httpResponse.statusCode);
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData. receivedData is an instance variable declared elsewhere.
if ([urlConnection isEqual:connection])
{
[receivedData appendData:data];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
// inform the user
NSLog(@"Connection failed! Error domain - %@, error code %d, error discryption - %@", [error domain], [error code], [error localizedDescription]);
// stop a CFRunLoop from running.
CFRunLoopStop(CFRunLoopGetCurrent());
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if ([urlConnection isEqual:connection] && (receivedStatusCode == 200))
{
NSString *filePath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
filePath = [rootPath stringByAppendingPathComponent:fileName];
NSLog(@"File Path %@", filePath);
NSString *data = [[[NSString alloc] initWithData:receivedData encoding:NSISOLatin1StringEncoding] autorelease];
NSLog(@"Data : %@", data);
[data writeToFile:filePath atomically:YES encoding:NSISOLatin1StringEncoding error:NULL];
}
else
{
}
[urlConnection release];
[receivedData release];
// stop a CFRunLoop from running.
CFRunLoopStop(CFRunLoopGetCurrent());
}
@end
答案 0 :(得分:1)
线程之间的通信是一个非常普遍的问题,并且 很多 可能的解决方案。
我看到的一个解决方案是不使用performSelectorInBackground:
,而是创建NSThread
实例,保存线程引用并调用start
。然后你可以使用[NSObject performSelector:onThread:...]
方法之一来调用stop方法(不要忘记在停止循环之前实际取消请求)。
另一种解决方案可能是在CFRunLoopRun
周期内调用while
超时,等待某些情况发生。请参阅iPhone: how to use performSelector:onThread:withObject:waitUntilDone: method?中的答案。
另一种解决方案是使用本地通知(NSNotificationCenter
)。后台线程可以注册自己进行通知,主线程可以在条件发生时发送它们。