Iphone 这是处理背景thred的正确方法吗?
我向服务器发出了很多请求:Iphone 这是处理背景thred的正确方法吗?,iphone,objective-c,ios,xcode,nsoperationqueue,Iphone,Objective C,Ios,Xcode,Nsoperationqueue,我向服务器发出了很多请求: NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [theRequest setValue:@"gzip" forHTTPHeaderField:@"Accept-
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[theRequest setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
urlData = [[NSMutableData data] retain];
urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
if (urlConnection)
{
finishedLoading = FALSE;
while(!finishedLoading) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
else
{
[delegate swHttpConnection:self withRequest:urlString failedWithError:nil];
}
…请求完成后,我会收到一个回调。但是,如果使用下面的代码,将不会调用选择器:
- (void)request:(MBRequest *)request finished:(NSMutableArray *)resultArray
{
//Handle resultArray data
[self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f];
}
…但是如果我使用下面的代码;它工作得很好:
- (void)request:(MBRequest *)request finished:(NSMutableArray *)resultArray
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void)
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
//Handle resultArray data
[self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f];
});
});
}
所以我的问题是双重的
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
我在某些方面得到了这样的警告:
Class _NSZombie_NSRunLoop is implemented in both ?? and ??
提前感谢我不认为
nsurlconnectionelegate
有一个请求:finished:
方法?通过查看您的代码,您可以使用它。如果是这样的话,我强烈认为您应该看看他们的网页,例如如何使用块执行此操作。我认为nsurlconnectionelegate
没有请求:完成:
方法?通过查看您的代码,您可以使用它。如果是这样的话,我强烈认为您应该看看他们的网页,例如如何使用块来完成此操作。当您更新此部分中的UI时
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void)
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
//Handle resultArray data
[self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f];
});
});
然后必须在UI线程中调用它,然后是,您必须在更新此段中的UI时执行此操作
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void)
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
//Handle resultArray data
[self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f];
});
});
然后必须在UI线程中调用它,然后是的,您必须这样做Pelase发布函数“update”的代码嗨,谢谢您的回复。update函数可以是任何东西。现在它只是一个空函数。我在里面放了一个NSLog,它从来没有被调用过“//Handle resultArray data”它是处理UI还是一些共享存储?是的,它更新了UI,包括一个tableView,并更新了一些本地存储。请发布函数“update”的代码。您好,感谢您的回复。update函数可以是任何东西。现在它只是一个空函数。我在其中放了一个NSLog,但它从未被调用“//Handle resultArray data”它是否处理UI或某个共享存储?是的,它更新了UI,包括一个tableView,并更新了一些本地存储