I do a lot of request to the server:
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];
}
…When the request is done, I get a callback. However, If I use the code below, the selector won’t get called:
- (void)request:(MBRequest *)request finished:(NSMutableArray *)resultArray
{
//Handle resultArray data
[self performSelector:@selector(someRandomFunction) withObject: nil afterDelay:1.0f];
}
..but if I use the code below; It works fine:
- (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];
});
});
}
So my question is twofold.
-
Is this the right way to do it? Do I need to force the callback to be run on the mainthred, or is the logic of the code wrong?
-
If I do have to force the callback to be ran on the mainthred, is the code above correct?
Also, the server request code crash sometimes at the:
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
And I get this warning somethings:
Class _NSZombie_NSRunLoop is implemented in both ?? and ??
Thanks in advance
As you update the UI in this segment
then it has to be called in the UI thread then yes you have to do it