【发布时间】:2014-03-03 06:05:34
【问题描述】:
我正在使用 NSOperation 队列来解析 url 并在完成后加载 UITableview,这是我的代码,我在 NSOperation 队列中使用 dispatch_async,我的问题是,我可以使用这种代码,这种方法是错误的吗?
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
{
res1.text=@"No available timeslot Found";
[self.view addSubview:res];
}
}
});
});
}
【问题讨论】:
-
你有什么问题?
标签: ios