如何在接收响应数据时重新加载ios

时间:2014-01-10 07:25:12

标签: ios uitableview httprequest

您好我的问题是我从Web服务获得响应,当我解析它并添加到表并重新加载表视图时,它不令人耳目一新。虽然如果我调用键盘中的[table reload],它会在那里更新。有人能告诉我我是否遗漏了什么

这就是我想要做的事情

- (void) longPoll {
//create an autorelease pool for the thread
   @autoreleasepool {

    NSLog(@"polling");
   VSAppDelegate *var = (VSAppDelegate*)[[UIApplication sharedApplication] delegate];
    //compose the request
NSError* error = nil;

NSHTTPURLResponse* response = nil;


    //send the request (will block until a response comes back)
NSData* responseData = [NSURLConnection sendSynchronousRequest:request                                           returningResponse:&response error:&error];
    NSLog(@"polling response is  %d",response.statusCode);

//pass the response on to the handler (can also check for errors here, if you want)
    [self performSelectorOnMainThread:@selector(dataReceived:) withObject:responseData waitUntilDone:YES];
}

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

- (void) startPoll {

[self performSelectorInBackground:@selector(longPoll) withObject: nil];
}
 - (void) dataReceived: (NSData*) theData 
{
    //process the response here
    NSError *error = nil;
    NSLog(@"polling data is %@",[[NSString alloc] initWithData:theData     encoding:NSUTF8StringEncoding]);
      NSLog(@"polling data is %@",[[theData base64EncodedString]base64DecodedString]);
   NSDictionary *notifDic= [NSJSONSerialization JSONObjectWithData:theData options:kNilOptions error:&error];

    //VSViewControllerSplit *split = [[VSViewControllerSplit alloc]init];
    [self RecieveFunction:notifDic];

}

2 个答案:

答案 0 :(得分:0)

试试吧

dispatch_async(dispatch_get_main_queue(), ^{

           [tablrView reloaddata];

                          });

答案 1 :(得分:0)

dataReceived方法似乎没有调用reloadData。我会假设RecieveFunction方法确实如此,但你应该确认一下。没看到RecieveFunction就很难说。

更基本的问题似乎是dataReceived方法正在创建VSViewControllerSplit的新实例,调用其RecieveFunction方法,然后让这个新的VSViewControllerSplit实例超出范围(如果使用ARC,除非你按下它,呈现它等,否则将被取消分配)。您可能不希望每次VSViewControllerSplit调用longPoll时都创建新dataReceived,而只是引用现有实例。