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];
}
}
});
});
}
你的問題是什麼? – bjb568