[英]Queues in iOS Objective-C
我正在尝试在iOS项目的Objective C
创建队列。 我希望活动监视器在主线程上运行并继续执行此操作,直到获得UITableView
所有数据为止。 这是我的代码
[super viewDidLoad];
// create the activity indicator in the main queue
dispatch_async(dispatch_get_main_queue(), ^{
UIActivityIndicatorView *ac = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[ac startAnimating];
});
MyQueue = dispatch_queue_create("com.example.gcd", NULL);
dispatch_async(MyQueue, ^{
self.client = [MSClient clientWithApplicationURLString:@"https://outnight-mobile.azure-mobile.net/" applicationKey:@"okYeRGfBagYrsbkaqWIRObeDtktjkF10"];
self.table = [self.client tableWithName:@"notifications"];
self.rowitems = [[NSMutableArray alloc] init];
MSQuery *query = [self.table query];
query.fetchLimit = 3;
[query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error)
{
//add the items to our local cop
self.rowitems = [items mutableCopy];
//[self.TableView reloadData];
}];
});
因此,您可以看到我为UIActivityIndiactor
创建了一个主线程,然后为我的sql工作创建了第二个线程。 指标不起作用,它直接进入UITableView
,甚至不进入myQueue进行分派。 这是否意味着我的UIActivityIndicitor
无法正常工作,所以它什么也不做? 我正在尝试使用GCD
以便可以在运行[tableview reload data]
之前准备好所有[tableview reload data]
。 有任何建议吗?
谢谢
杰森
您的代码不会“创建主线程”。 viewDidLoad
已经在主线程上被调用,因此您只是在排队等待稍后在主线程上执行的块。 该块创建UIActivityIndicatorView
的实例,然后将其销毁(因为您没有将其添加为子视图或存储对其的引用)。
删除该块,将其添加为子视图,并将其引用存储在@property中。
您的第二个代码块将代码排队在后台线程上运行(尽管它不会直接创建该线程)。 此块还应该是:
UIActivityIndicatorView
但:
您正在使用MSQuery
。 IIRC这需要从主线程运行。 它会自动在后台线程上运行所有网络,并回调(完成块)到主线程。
因此,您可能应该删除GCD的其他用途,而只是修改活动指示器并重新加载表格视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.