2014-03-03 95 views
0

我正在使用NSOperation隊列解析一個url並在完成後加載UITableview,這裏是我的代碼,我在NSOperation隊列中使用dispatch_async,我的問題是,我可以使用這種類型的代碼,這種方法錯誤?dispatch_async與NSOperation隊列

queue = [[NSOperationQueue alloc]init]; 
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self 
                        selector:@selector(loadDataWithOperation) object:nil]; 
     [queue addOperation:operation]; 

- (void) loadDataWithOperation { 

    __block NSData *data = [[NSData alloc] init]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ 

     NSURL *dataURL = [NSURL URLWithString:[NSString stringWithFormat:@"url"]]; 
     NSError *err=nil; 
     data=[NSData dataWithContentsOfURL:dataURL options:NSDataReadingUncached error:&err]; 

     dispatch_async(dispatch_get_main_queue(), ^(void){ 

      NSError *err=nil; 

      NSDictionary *response_login; 


      if(!err) 
      { 
       [bookingTime removeAllObjects]; 
       [duration removeAllObjects]; 
       [guestNumber removeAllObjects]; 
       [status removeAllObjects]; 

       response_login = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err]; 
       NSArray *TimeSlotList=[[response_login objectForKey:@"ResultInfo"]objectForKey:@"TimeSlotList"]; 

       if([TimeSlotList count]>0) 
       { 
        for(NSDictionary *dict in TimeSlotList) 
        { 

         [bookingTime addObject:[dict objectForKey:@"BookingTime"]]; 
         [duration addObject:[dict objectForKey:@"Duration"]]; 
         [status addObject:[NSString stringWithFormat:@"%@",[dict objectForKey:@"IsBlocked"]]]; 
         [guestNumber addObject:[dict objectForKey:@"AvailCount"]]; 


        } 


        [self.view addSubview:bookingDetails]; 
        [self.bookingDetails reloadData]; 
       } 
       else 
       { 

        [email protected]"No available timeslot Found"; 
        [self.view addSubview:res]; 

       } 
      } 


     }); 
    }); 

}

+0

你的問題是什麼? – bjb568

回答

0

以這種方式使用的NSOperationQueue顯得毫無意義。

您似乎也有直接訪問實例變量而不是訪問器的習慣。您應該使用下劃線字符來啓動所有實例變量,以便讀者閱讀代碼時非常明顯。使用實例變量而不是訪問器可能會導致很難找到的錯誤。

相關問題