NSURLConnection是否需要NSRunLoop才能执行? [英] NSURLConnection needs a NSRunLoop to execute?
问题描述
我正在尝试从名为connect的方法中获取URL的内容.它读取其配置设置并尝试获取URL.似乎需要运行循环才能执行.我虽然只执行一次就可以完成.似乎我不需要使运行循环运行,除了在每次调用connect时一次获取此URL之外.有更好的方法吗?
I'm trying to fetch the contents of a URL from a method called connect. It reads its configuration settings and attempts to fetch the URL. It appears to need the run loop to execute. I though it would just execute once and be done. It seems that I don't need to have that run loop running for any reason other than to fetch this URL once, whenever connect is called. Is there a better way do do this?
- (BOOL) connect
{
// read serverName and eventId from preferences
NSMutableArray* preferences;
preferences = [NSMutableDictionary dictionaryWithContentsOfFile:@"/tmp/wt.plist"];
// if these values aren't nil or blank
if ([preferences valueForKey:@"serverAddress"] && [preferences valueForKey:@"eventId"]) {
[self setServerAddress:@"172.16.28.210"];
[self setEventId:@"OT-3037009"];
} else{
// return some kind of error message
}
NSLog(@"VideoDataURL: %@", [self getVideoDataURL]);
// grab the URL and query the server
NSURL *myURL = [NSURL URLWithString:@"http://localhost"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:2];
__unused NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[[NSRunLoop currentRunLoop] run];
return TRUE;
}
- (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.
// receivedData is an instance variable declared elsewhere.
[incomingData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
/*
* Called each time a chunk of data arrives
*/
NSLog(@"received %lu bytes", [data length]);
// Create a mutable data if it doesn't already exist
if (!incomingData) {
incomingData = [[NSMutableData alloc] init];
}
[incomingData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
/*
* Called if the connection fails
*/
NSLog(@"connection failed: %@", [error localizedDescription]);
incomingData = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
/*
* Called when the last chunk has been processed
*/
NSLog(@"Got it all!");
NSString *string = [[NSString alloc] initWithData:incomingData encoding: NSUTF8StringEncoding];
incomingData = nil;
NSLog(@"response is: %@", string);
}
推荐答案
如果要从主线程启动连接,是的,这是正确的.我只是使用GCD在主线程上创建和启动连接,而不必考虑运行循环.
If you're starting the connection off of the main thread, yes, this is correct. I just use GCD to create and start the connection on the main thread to avoid having to think about run loops.
dispatch_async(dispatch_get_main_queue(), ^{
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[conn start];
});
可以在任何地方工作,而不必跟踪运行循环或线程.
Works from anywhere and you don't have to keep track of run loops or threads.
这篇关于NSURLConnection是否需要NSRunLoop才能执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!