3

I'm using the code below to test asynchronous calls using AFNetworking in my unit tests for iOS7. When I run the test alone, or even the entire test case, the code works fine. However, when I run the entire test suite, I get EXC_BAD_ACCESS (code=1, address0x7a) on the line [theRL runMode:NSDefaultRunLoopMode beforeDate:date];

  NSRunLoop *theRL = [NSRunLoop currentRunLoop];

    //setup timeout
    float waitIncrement = 0.1f;
    int timeoutCounter  = (int)(60 / waitIncrement); //30 sec timeout
    BOOL controlConditionReached = NO;


    // Begin a run loop terminated when the downloadComplete it set to true
    while (controlConditionReached == NO)
    {
        NSDate* date = [NSDate dateWithTimeIntervalSinceNow:waitIncrement];

        //Here: EXC_BAD_ACCESS (code=1, address0x7a) when running the entire test suite
        [theRL runMode:NSDefaultRunLoopMode beforeDate:date];
        //control condition is set in one of your async operation delegate methods or blocks
        controlConditionReached = self.downloadComplete || self.downloadFailed||self.cachedVideoAvailable ;

        //if there's no response - timeout after some time
        if(--timeoutCounter <= 0)
        {
            break;
        }
    }

I see that at the same time as I'm using my loop, AFNetoworking is using this method:

+ (void)networkRequestThreadEntryPoint:(id)__unused object {
    @autoreleasepool {
        [[NSThread currentThread] setName:@"AFNetworking"];

        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [runLoop run];
    }
}

How can I identify what is causing this error? If I change my method's run loop mode to NSRunLoopCommonModes, then all of my tests fail

Alex Stone
  • 46,408
  • 55
  • 231
  • 407

0 Answers0