我正在使用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];
}
}
});
});
}
答案 0 :(得分:0)
以这种方式使用NSOperationQueue似乎毫无意义。
您似乎也有直接访问实例变量而不是访问者的习惯。您应该使用下划线字符启动所有实例变量,以使读取代码的任何人都能看到它。使用实例变量而不是访问器可能会导致很难找到的错误。