iOS方法在运行时被调用时崩溃(使用NSOperationQueue)

iOS方法在运行时被调用时崩溃(使用NSOperationQueue),ios,objective-c,uitableview,nsoperation,nsoperationqueue,Ios,Objective C,Uitableview,Nsoperation,Nsoperationqueue,我有一个UIViewController,它包含一个UITableView,当我调用方法requireObjects时,最后一个方法填充数组以配置表。我声明cellForRowAtIndexPath方法工作正常,并根据[\u arrID count]将多个单元格出列 每个填充的数组都有一个与所有其他数组匹配的索引。例如,arrID[5]=453对应于arrNames[5]=Jean,对应于arrStyle[5]=Anger,对应于arrLocation[5]=New York,对应于arrPro

我有一个UIViewController,它包含一个UITableView,当我调用方法requireObjects时,最后一个方法填充数组以配置表。我声明cellForRowAtIndexPath方法工作正常,并根据[\u arrID count]将多个单元格出列

每个填充的数组都有一个与所有其他数组匹配的索引。例如,arrID[5]=453对应于arrNames[5]=Jean,对应于arrStyle[5]=Anger,对应于arrLocation[5]=New York,对应于arrProfileSnaps[5]=Jean.png

例如,这5个数组构成了tableView中的第五个单元格。方法requireObjects成功,并且正确构建了表。当我在这个方法仍在运行时调用它时,问题就出现了。我希望当我单击按钮时,该方法停止并重复。或者在绝望的时候,该方法结束执行并在我单击按钮时重复也很好。但不幸的是,我的应用程序崩溃了。代码如下:

- (void)requireObjects{

    @try {
        //threadsQueue is a NSOperationQueue allocated and initialized in viewDidLoad method
        [threadsQueue cancelAllOperations];
        [threadsQueue waitUntilAllOperationsAreFinished];



                [_arrID removeAllObjects];
                [_arrNames removeAllObjects];
                [_arrStyle removeAllObjects];
                [_arrLocation removeAllObjects];
                [_arrProfileSnaps removeAllObjects];

            _arrNames = [[NSMutableArray alloc] init];
            _arrStyle = [[NSMutableArray alloc] init];
            _arrLocation = [[NSMutableArray alloc] init];
            _arrProfileSnaps = [[NSMutableArray alloc] init];

        [threadsQueue addOperation:[NSBlockOperation blockOperationWithBlock: ^{

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                CGRect activityFrame = CGRectMake(self.view.center.x, self.view.center.y, 0.0, 0.0);

                _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:activityFrame];

                [[self.view superview] addSubview:_activityIndicator];

                _activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
                _activityIndicator.color = [UIColor whiteColor];
                [_activityIndicator startAnimating];
                [_tableView reloadData];
            }];


        _arrID = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]];

            for (int i = 0; i < [_arrID count]; i++) {
                [_arrStyle addObject:@“..”];
                [_arrNames addObject:@".."];
                [_arrLocation addObject:@“..”];
                [_arrProfileSnaps addObject:[UIImage imageNamed:@"blank"]];
            }

            [[NSOperationQueue mainQueue] addOperationWithBlock: ^{

                [self.tableView reloadData];
                [_activityIndicator stopAnimating];
            }];
        }]];
    }

    @catch (NSException *exception) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"An exception has occurred" message:[NSString stringWithFormat:@"%@",exception] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
        [alert show];
    }
}
应用程序在for循环的一次迭代中,在我执行[u arrNames insertObject:]时崩溃……错误:

由于未捕获异常“NSRangeException”而终止应用程序,原因: '*-[\uu NSArrayM objectAtIndex:]:索引0超出了空的界限 数组'


奇怪的是,每当我调用该方法时,它都会成功,但只有在它已经运行时调用它时才会崩溃!

您正在调用[\u arrID removeAllObjects];对于操作队列上的_arrNames等也一样,这可能会并发运行,具体取决于调用了哪个thread RequireObject:且不一定是线程安全的。在创建块之前,请尝试初始化和设置对象,并使用变量传递,而不是通过存储在对象中的变量进行通信。

在一个带有数组索引越界错误的问题中,请不要注释填充数组的方式,并期待得到任何帮助。我无法向您展示如何填充数组,但我说数组已正确填充。我告诉了。我检查了所有内容,甚至使用了NSLog。它显然没有正确填充。当您希望它为b时,它在某个点上是空的e不为空。请先添加异常断点。然后正确调试代码,并查看数组中是否有对象。异常断点将告诉您崩溃的确切位置。问题在数组中。我已尝试过此操作…没有任何内容请参见代码中的编辑!但问题仍然存在!没有其他解决方案?