dispatch_async上未捕获的异常

时间:2014-01-28 06:03:03

标签: uitableview ios7 objective-c-blocks grand-central-dispatch

我收到了一个未被捕获的错误,我相信它是在一个区块内(iOS 7)。但是因为NSException只显示在调试日志上,所以我很难知道导致它的原因。

这是我的代码:

-(void)insertIntoArray:(NSArray *)array
{
@try {
    if([array count] > 0){
        NSArray *anArray = [array copy];
        dispatch_queue_t q = dispatch_queue_create("getCustomerList", NULL);
        dispatch_async(q, ^(void){
            for (NSDictionary *dict in anArray){
                Customer *customer = [[Customer alloc]init];
                NSInteger i = [[dict objectForKey:@"CustomerID"] integerValue];
                customer.ID = i;
                customer.AppointmentID = [dict objectForKey:@"AppointmentID"];
                customer.UID = [dict objectForKey:@"UID"];
                customer.AccountNumber = [dict objectForKey:@"AccountNumber"];
                customer.Birthdate = [dict objectForKey:@"Birthdate"];
                customer.Gender = [dict objectForKey:@"Gender"];
                customer.FirstName = [dict objectForKey:@"Firstname"];
                customer.MiddleName = [dict objectForKey:@"MiddleName"];
                customer.LastName = [dict objectForKey:@"LastName"];
                customer.Address = [dict objectForKey:@"address1"];
                customer.PhoneNumber = [dict objectForKey:@"PhoneNumber"];
                customer.PictureName = [dict objectForKey:@"PictureName"];
                customer.LastVisit= [dict objectForKey:@"LastVisit"];
                customer.AccountOpened = [dict objectForKey:@"AccountOpened"];
                customer.PhoneNumber2 = [dict objectForKey:@"HPNum"];
                customer.DisplayName = [dict objectForKey:@"DisplayName"];
                customer.ProfileImage = [dict objectForKey:@"ProfileImage"];
                customer.EmailAddress = [dict objectForKey:@"EmailAddress"];
                customer.Allergy = [dict objectForKey:@"Allergy"];

                char index = [[dict objectForKey:@"DisplayName"] characterAtIndex:0];
                NSString *uniChar = [NSString stringWithFormat:@"%c",index];
                if(![self.arrayOfIndices containsObject:uniChar]){
                    [self.arrayOfIndices addObject:uniChar];
                }
                [self.arrayOfCustomer addObject:customer];
            }

            self.segmentedControl.userInteractionEnabled = YES;
            [self.indicator stopAnimating];
            [self.indicator removeFromSuperview];
            [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
            self.isRefreshing = NO;
            [self.tableView setUserInteractionEnabled:YES];
            self.tableView.scrollEnabled = YES;
            self.navigationItem.rightBarButtonItem.enabled = true;
            [self.refreshControl endRefreshing];
            [self.tableView setContentOffset:CGPointMake(0.0, 0.0) animated:NO];


        });
    }
    else {
        [self.indicator stopAnimating];
        [self.indicator removeFromSuperview];
        self.isRefreshing = NO;
        [self.tableView setUserInteractionEnabled:YES];
        self.tableView.scrollEnabled = YES;
        self.navigationItem.rightBarButtonItem.enabled = true;
        [self.refreshControl endRefreshing];

    }
}
@catch (NSException *exception) {
    NSLog(@"Error %@",exception);
    [self alertStatus:[NSString stringWithFormat:@"An unexpected error has occured. Exception %@", exception.reason] :@"Error!"];
}
self.navigationItem.rightBarButtonItem.enabled = true;

}

这是NSException在日志中的样子

enter image description here

我的问题是,有没有办法让我从块中获取异常原因或描述?如果没有,我的代码中是否有任何错误导致错误?

如果有人能提供帮助,我会很高兴。干杯!

1 个答案:

答案 0 :(得分:0)

好的,我已经阅读了一些关于GCD的内容,我想出了这个解决方案。我不知道这是不是正确的方式。但它现在有效。

-(void)insertIntoCustomerArray:(NSArray *)array
{
@try {
    [self.arrayOfCustomers removeAllObjects];
    if([array count] > 0) {

        __block LoginViewController *blockSelf = self;
        dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(concurrentQueue, ^{
            __block NSArray *blockArray = [array copy];

            dispatch_sync(concurrentQueue, ^{
                for (NSDictionary *dict in blockArray){
                    Customer *customer = [[Customer alloc]init];
                    NSInteger i = [[dict objectForKey:@"CustomerID"] integerValue];
                    customer.ID = i;
                    customer.AppointmentID = [dict objectForKey:@"AppointmentID"];
                    customer.UID = [dict objectForKey:@"UID"];
                    customer.AccountNumber = [dict objectForKey:@"AccountNumber"];
                    customer.Birthdate = [dict objectForKey:@"Birthdate"];
                    customer.Gender = [dict objectForKey:@"Gender"];
                    customer.FirstName = [dict objectForKey:@"Firstname"];
                    customer.MiddleName = [dict objectForKey:@"MiddleName"];
                    customer.LastName = [dict objectForKey:@"LastName"];
                    customer.Address = [dict objectForKey:@"address1"];
                    customer.PhoneNumber = [dict objectForKey:@"PhoneNumber"];
                    customer.PictureName = [dict objectForKey:@"PictureName"];
                    customer.LastVisit= [dict objectForKey:@"LastVisit"];
                    customer.AccountOpened = [dict objectForKey:@"AccountOpened"];
                    customer.PhoneNumber2 = [dict objectForKey:@"HPNum"];
                    customer.DisplayName = [dict objectForKey:@"DisplayName"];
                    customer.ProfileImage = [dict objectForKey:@"ProfileImage"];
                    customer.EmailAddress = [dict objectForKey:@"EmailAddress"];
                    customer.Allergy = [dict objectForKey:@"Allergy"];

                    char index = [[dict objectForKey:@"DisplayName"] characterAtIndex:0];
                    NSString *uniChar = [NSString stringWithFormat:@"%c",index];
                    if(![blockSelf.arrayOfIndices containsObject:uniChar]){
                        [blockSelf.arrayOfIndices addObject:uniChar];
                    }
                    [blockSelf.arrayOfCustomers addObject:customer];

                }

            });

            dispatch_async(dispatch_get_main_queue(), ^{
                 [self removeIndicatorView];
            });
        });
    }
    else {

    }
}
@catch (NSException *exception) {
    NSLog(@"Error %@", exception.reason);
    [self alertStatus:[NSString stringWithFormat:@"Error while fetching the customer list. Exception %@", exception] :@"Error!"];
}

}