目标C,在异步操作后更新表视图

时间:2014-03-17 15:50:29

标签: ios objective-c uitableview asynchronous

我正在运行一个异步操作,它循环遍历一个json数据文件,然后应该更新一个表视图。

这是我使用的代码。

-(void) TopDealsRecieve:(NSString *)Devicetoken;
{

    currentcount = 0;

    allcontent = newArray;
    //Send device token and retireve TopDeals
    NSString *mainURL = @"http://myurl.com/api/";
    NSString *firstprefix = @"type=apicall&device=";
    NSString *deviceIDforURL = [NSString stringWithFormat:@"%@", Devicetoken];
    NSString *stringToGoToEncoder = [NSString stringWithFormat: @"%@%@", firstprefix, deviceIDforURL];
    NSData *plainData = [stringToGoToEncoder dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [plainData base64EncodedStringWithOptions:0];
    NSString *returnURL = [NSString stringWithFormat:@"%@%@", mainURL, base64String];
    NSURL *returncompletedURL = [[NSURL alloc] initWithString:returnURL];

    //Retrieve Dictionary of Top Deals
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:returncompletedURL];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        //Get all output and store for segue
        NSLog(@"%@", responseObject);
        currentcount++;
        numrows = [responseObject valueForKey:@"rows"];
        numrowscompare = [numrows integerValue];
        allLogos = [[responseObject valueForKey:@"data"] valueForKey:@"logo"];
        allcontent = [[responseObject valueForKey:@"data"] valueForKey:@"content"];
        allpostode = [[responseObject valueForKey:@"data"] valueForKey:@"postcode"];
        NSLog(@"%lu", (unsigned long)allcontent.count);
        allname = [[responseObject valueForKey:@"data"] valueForKey:@"name"];
        alladdress = [[responseObject valueForKey:@"data"] valueForKey:@"address"];
        alladdress2 = [[responseObject valueForKey:@"data"] valueForKey:@"address2"];
        alllat = [[responseObject valueForKey:@"data"] valueForKey:@"lat"];
        alllong = [[responseObject valueForKey:@"data"] valueForKey:@"lng"];
        allstart = [[responseObject valueForKey:@"data"] valueForKey:@"start"];
        allfinish = [[responseObject valueForKey:@"data"] valueForKey:@"finish"];
        allstartnice = [[responseObject valueForKey:@"data"] valueForKey:@"nicestart"];
        allfinishnice = [[responseObject valueForKey:@"data"] valueForKey:@"nicefinish"];
        // hide the ticker

        [self.tableview reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        // 4
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Top Deals check internet connection!"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];

    [operation start];

}

运行操作时,所有数据都可以正常运行到控制台,但表视图不会使用正确的数据进行更新。在调用函数之前,它会使用all content数组中的数据进行更新。

似乎来自服务器的数据没有足够快地传递到表视图。在某种队列中完成操作后,我需要以某种方式将表视图设置为reloadData。

[self.tableview reloadData]; 

对不起,如果不太清楚,任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

如果allContent是您NSArray使用的UITableViewDatasource,那么您永远不会在completionBlockWithSuccess中更新它,所以这不是异步更新的问题,而是一个简单的错误:更新在致电allContent之前reloadData

(另外一条评论:使用'全局'数组很糟糕!)