0

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.

  1. 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?

  2. 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

BlackMouse
  • 4,442
  • 6
  • 38
  • 65

2 Answers2

2

I don't think NSURLConnectionDelegate has a request:finished: method? and by looking at your code it seams as your using MBRequest. If so I strongly think you should have a look at their webpage for example how to do this using blocks.

Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57
1

As you update the UI in this segment

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

then it has to be called in the UI thread then yes you have to do it

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56