我不得不使用异步NSURLConnection内部的NSOPeration在后台模式,因为它的反应是有大的dataI必须避免苹果的有限长度编码在didEnterBackground.instead中使用它的我使用以下代码通过NSOperation与NSInvocation作为,但它不是working.connectToServer是有NSURLConnection operation.any帮助吗?
-(void)viewDidLoad
{
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(connectServer)
object:nil];
[queue addOperation:operation];
[operation release];
[queue autorelease];}
-(void)connectServer
{
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}}
发布于 2012-02-15 09:13:52
嗯,也许你可以在一个块中为主队列做连接,如下所示:
dispatch_async(dispatch_get_main_queue(), ^{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0];
_connection = [[NSURLConnection alloc] initWithRequest:request startImmediately:YES];
[request release];
});然后应该调用委托方法。
发布于 2012-02-10 19:13:10
每当您希望在辅助线程上运行NSURLConnection时,都需要将该连接添加到运行循环中
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self
startImmediately:YES];
[request release];
[_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_connection start];
[pool release];发布于 2012-02-10 18:50:15
这一点我可能错了,但我还是会尝试。
医生说..。for start方法
start使连接开始加载数据(如果尚未加载数据)。
因此,在我看来,您必须手动启动连接,因为它在操作队列中。如果我错了,请纠正我。
https://stackoverflow.com/questions/9226215
复制相似问题