0

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

在下一个视图控制器的 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];
        }    
 }

请指导以上,我使用的是正确的方法还是有其他方法可以做到这一点。提前致谢。

4

3 回答 3

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
    });
});
于 2013-07-22T12:48:48.747 回答
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];
});
于 2013-07-22T12:54:11.607 回答
0

you can use GCD

   dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        //Fire api
        dispatch_async(dispatch_get_main_queue(), ^{
          //update ui
        });
    });
于 2013-07-22T12:46:36.787 回答