gpt4 book ai didi

objective-c - NSOperation 内的 ASynchronous NSURLConnection 与 NSInvocation?

转载 作者:行者123 更新时间:2023-11-28 20:39:20 24 4
gpt4 key购买 nike

我必须在后台模式下使用 NSOPeration 中的异步 NSURLConnection,因为它的响应有大量数据我必须避免在 didEnterBackground 中使用 Apple 的有限长度编码。而不是我通过 NSOperation 和 NSInvocation 使用以下代码,但它是不工作。connectToServer 正在执行 NSURLConnection 操作。有什么帮助吗?没有调用 didReceiveData、didReceiveResponse 委托(delegate)方法吗?

 -(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");
}
}

最佳答案

嗯,也许你可以在主队列的一个 block 内用这个连接:

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];
});

然后应该调用委托(delegate)方法。

关于objective-c - NSOperation 内的 ASynchronous NSURLConnection 与 NSInvocation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9226215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com