如何在后台从Web服务获取数据,然后更新UI

时间:2013-07-22 12:39:19

标签: iphone ios6

我正在做的是应用推视图控制器从表视图的didSelectMethod移动到下一个视图。在下一个视图中,为该视图提取控制器数据。因此,问题是视图仅在完全获取数据时切换,并且它包含图像和文本。但我已经为图像应用了延迟加载,但问题是立即转移到下一个视图,然后获取数据并更新UI和tableview。以下是我正在尝试的代码。

在下一个视图控制器的didLoad方法上。

 [NSThread detachNewThreadSelector:@selector(setImage) toTarget:self withObject:nil];

方法setImage获取所有数据图像和文本。

-(void)setImage
{    
        if([type isEqualToString:@"Organisation"])
        {
            self.mGetDataDict = [MyEventApi members:self.mUserIdDict];
            self.mRecievedDataDict = [self.mGetDataDict valueForKey:@"members"];
        }
        if([type isEqualToString:@"Individual"]){
            self.mGetDataDict = [MyEventApi friends:self.mUserIdDict];
            self.mRecievedDataDict = [self.mGetDataDict valueForKey:@"friends"];
        }

        if([self.mGetDataDict valueForKey:@"friends"] == [NSNull null])
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"alert" message:@"You have not added any friend yet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        else
        {
            self.mFrndNameArr = [[NSMutableArray alloc]init];
            self.mFrndImgArr = [[NSMutableArray alloc]init];
            self.mFirstNameArr = [[NSMutableArray alloc]init];
            self.mLastNameArr = [[NSMutableArray alloc]init];
            self.mFrndIdArr = [[NSMutableArray alloc]init];
            self.mFrndMSinceArr = [[NSMutableArray alloc]init];
            self.mFrndDescArr = [[NSMutableArray alloc]init];

            self.mFrndNameArr = [self.mRecievedDataDict valueForKey:@"username"];
            self.mFrndImgArr = [self.mRecievedDataDict valueForKey:@"image"];
            self.mFirstNameArr = [self.mRecievedDataDict valueForKey:@"firstName"];
            self.mLastNameArr = [self.mRecievedDataDict valueForKey:@"lastName"];
            self.mFrndIdArr = [self.mRecievedDataDict valueForKey:@"id"];
            self.mFrndMSinceArr = [self.mRecievedDataDict valueForKey:@"memberSince"];
            self.mFrndDescArr = [self.mRecievedDataDict valueForKey:@"description"];

             [self.mFriendsTable reloadData];
        }    
 }

请指导以上,我是使用正确的方法还是有另一种方法来做到这一点。 提前谢谢。

3 个答案:

答案 0 :(得分:1)

请使用GCD:)

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    //in this scope load your data from server
    dispatch_async(dispatch_get_main_queue(), ^{
      //use this scope to reload UI with data received from scope above
      // in your case this is NSDictionary object, this object will be able to use in this scope because this scope will catch variables from scope above
    });
});

答案 1 :(得分:0)

你可以使用GCD

   dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        //Fire api
        dispatch_async(dispatch_get_main_queue(), ^{
          //update ui
        });
    });

答案 2 :(得分:0)

我建议不要在您的上下文中使用NSThread API。像GCD或NSOperation这样的API处于更高的水平,并将为您提供您正在寻找的解决方案:

使用GCD的实现将如下所示

dispatch_queue_t backgroundQueue = dispatch_queue_create("backgroundQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(backgroundQueue, ^{
    // update your method to remove the table refresh
    [self setImage];

dispatch_async(dispatch_get_main_queue(), ^{
    //Update the UI on the main thread
    [self.mFriendsTable reloadData];
});