[[NSRunLoop currentRunLoop] runMode:* beforeDate:*];中的BAD EXC;

时间:2013-06-10 20:35:47

标签: ios objective-c unit-testing exc-bad-access

我正在尝试使用单元测试对我的应用进行一些压力测试,而且我遇到了一些问题。以下是我的代码:

    //Stress test api and core data
__block BOOL done = NO;
for (int i = 0; i < 100 ; i++) {
    DLog(@"in here");
    [viewController createList:testList
                       success:^(Lists *list) {
                           DLog(@"in success block : %d", i);
                           STAssertNotNil(list, @"list is not nil");
                           done = (i == 99);
                       }
                       failure:^(NSError *error) {
                           DLog(@"in fail block");
                       }
     ];        
}

@autoreleasepool {

    while (!done) {
        // This executes another run loop.
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
}

问题是,经过几次迭代后,我在行上遇到错误的访问错误

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

由于this post,我已将while循环放在@autorelease中。我在项目中使用 ARC ,所以我不知道这是否会导致问题..?我需要使用NSRunLoop强制单元测试等待块完成。

有没有人遇到过这个问题?

1 个答案:

答案 0 :(得分:2)

在类似的情况下,我做的不同,它工作得很好。我做的是在主测试中,使用dispatch_group_async到正常队列,然后在主测试中等待组完成。这样做很好,你不会搞乱单元测试的runloop。

如果出于某种原因上述情况不可接受,请尝试不使用任何自动释放池来查看是否有效如果有,请将其移至while看。使用ARC,对自动释放池的需求大大减少。您也可以使用Instruements观察内存使用情况。