0

当我调用“performSelectorInBackground”来异步处理某些操作时。

我的代码结构是这样的:

[self performSelectorInBackground:@selector(AddTheAdditionalDataSourceToTableView) withObject:nil];

AddTheAdditionalDataSourceToTableView 的代码结构如下:

-(void)AddTheAdditionalDataSourceToTableView
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    ...
        dispatch_async(dispatch_get_current_queue(), ^{
        ...
        });
    ...
    });
}

在viewController调用'popViewController'之后,永远不会调用dealloc!!!

我知道我可以直接调用“AddTheAdditionalDataSourceToTableView”。它将异步处理并且工作正常。当 viewController 消失时会调用 dealloc。但我不知道为什么使用“performSelectorInBackground”调用它会导致上面的这个异常。

: (

- (void)reloadTableViewDataSource
{
    //  should be calling your tableviews data source model to reload
    //  put here just for demo
    [self performSelectorInBackground:@selector(reFreshTableView) withObject:nil];
    //[self reFreshTableView];

}

- (void)reFreshTableView
{

NSString *myPoint = [self currentLocationPoint];
if (!myPoint || [myPoint isEqualToString:@""]) {
    [self enableButtons];
    [tableView tableViewDidFinishedLoading];
    return;
}

Interface *interface = [[Interface alloc]init];
/* 默认从page 1开始刷新数据 */
[self disableButtons];

_interface = (id)interface;
interface.delegate = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    [interface aroundInfoFromServiceInterface:_distanceType
                                withTableType:_tableType
                                 withSortType:_sortType
                                  withPageNum:1
                               withNumPerPage:NumberOfCellInOneRequest withPoint:myPoint];
});
//[interface release];
//[tableView flashMessage:@"hahahah"];
}

- (void)aroundInfoFromServiceInterface:(NSInteger)distance
                     withTableType:(NSInteger)tableType
                      withSortType:(NSInteger)sortType
                       withPageNum:(NSInteger)pageNum
                    withNumPerPage:(NSInteger)numPerPage
                         withPoint:(NSString *)myPiont
{

RequestData *data1 = [RequestData requestData:[NSString stringWithFormat:@"%d", distance] key:@"aroundDistant"];
RequestData *data2 = [RequestData requestData:[NSString stringWithFormat:@"%d", tableType] key:@"itemType"];
RequestData *data3 = [RequestData requestData:[NSString stringWithFormat:@"%d", sortType] key:@"sortType"];
RequestData *data4 = [RequestData requestData:[NSString stringWithFormat:@"%d", pageNum] key:@"pageNum"];
RequestData *data5 = [RequestData requestData:[NSString stringWithFormat:@"%d", numPerPage] key:@"numPerPage"];
RequestData *data6 = [RequestData requestData:[NSString stringWithFormat:@"%@", myPiont] key:@"mappoint"];
NSLog(@"%@ , %@ , %@ , %@ , %@ , %@" , [NSString stringWithFormat:@"%d", distance] ,[NSString stringWithFormat:@"%d", tableType] ,[NSString stringWithFormat:@"%d", sortType] ,  [NSString stringWithFormat:@"%d", pageNum], [NSString stringWithFormat:@"%d", numPerPage] , [NSString stringWithFormat:@"%@", myPiont]);
NSArray *tempArray = [NSArray arrayWithObjects:data1, data2, data3, data4, data5, data6, nil];

DataFromService *dataFromService = [[[DataFromService alloc]init]autorelease];

dispatch_async(dispatch_get_main_queue(), ^{

    NSString *strContent = [dataFromService requestData:tempArray fromURL:tableType == 0?AROUND_FRIEND_INFO_URL:AROUND_FISHING_STORE_AND_AREA_INFO_URL];

    NSArray *responseArray = (NSArray *)[self checkRequestResponse:strContent];
    if (responseArray) {
        NSMutableArray *tempArray = [[NSMutableArray alloc]init];
        switch (tableType) {
            case TableType_FrinedsAround:
                tempArray = [[FormatData shareInstance] formatDictToFriendsAround:responseArray];
                break;
            case TableType_FishingStoreAround:
                tempArray = [[FormatData shareInstance] formatDictToFishingStoreAround:responseArray];
                break;
            case TableType_FishingAreaAround:
                tempArray = [[FormatData shareInstance] formatDictToFishingAreaAround:responseArray];
                break;
            default:
                break;
        }

        if (self.delegate && [self.delegate respondsToSelector:@selector(requestDataSuccess:responses:)]) {
            [self.delegate requestDataSuccess:self responses:tempArray];
            return;
        }
    }
});
}
4

1 回答 1

0
dispatch_get_current_queue()

获取后台队列,因为您使用的是 performSelectorInBackground,所以它从后台队列中调用。

dispatch_get_main_queue()

dispatch_async 立即返回,因此您不需要使用 performSelectorInBackground:。您可以直接调用 dispatch_async :

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
...
    dispatch_async(dispatch_get_main_queue(), ^{
    ...
    });
...
});
于 2013-05-21T14:08:05.340 回答