【发布时间】:2012-08-29 20:03:09
【问题描述】:
我正在尝试使用 NSURLConnection 通过 HTTP 获取图像。我在后台执行选择器
[self performSelectorInBackground:@selector(loadingDaemon) withObject:nil];
我在哪里做这个
NSString *fullImageURL = [NSString stringWithFormat:@"http://%@/%@", baseURL, currentlyLoadingImage];
NSURL *imgURL = [NSURL URLWithString:fullImageURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:imgURL];
request.timeoutInterval = 10;
NSURLConnection *newConnection = [NSURLConnection connectionWithRequest:request delegate:self];
我将 NSURLConnection 的委托设置为 self 并实现方法
connection:didReceiveResponse:
connection:didReceiveData:
connection:didFailWithError:
connectionDidFinishLoading:
此外,我记录了每个方法调用和条件分支。但是上面的 4 个函数永远不会被调用。请你解释一下为什么?
【问题讨论】:
-
我怀疑你创建的 URL 包含特殊字符而没有被转义,所以它是无效的。
-
如果你在方法结束时说
[[NSRunLoop currentRunLoop] run]会发生什么? -
您知道,使用 NSURLConnection 异步加载的一个主要原因是您可以在主线程上完成所有这些操作,而不必在后台运行。
-
2 H2CO3 - 测试 URL 是“google.com/images/icons/product/chrome-48.png”,我查看了 :) 2 Kevin - 非常感谢!它正在工作! :) 2 Carl - 是的,但我也必须在后台控制连接,因为我在后台加载了很多图像
标签: objective-c ios multithreading networking selector