我正在尝试确保单例在UI更新之前完成执行。我尝试使用alertView显示请等待并在完成后将其解雇,但它不起作用。然后,当适配器更改状态时,我尝试使用通知中心更新UI中的标签。
点击按钮并等待单身人士完成连接的最佳方式是什么?我应该在dispatch_get_main_queue中移动_adapter和_transporter吗?
- (IBAction)connectAdapter:(id)sender {
// connnect to adapter
dispatch_queue_t myQueue = dispatch_queue_create("my queue", NULL);
dispatch_async(myQueue, ^{
[BLEManager sharedInstance];
NSLog(@"DID RUN BLE MANAGER");
_adapter = BLEManager.sharedInstance.adapter;
_transporter = BLEManager.sharedInstance.transporter;
dispatch_async(dispatch_get_main_queue(), ^{
//update your UI
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAdapterChangedState:) name:AdapterDidUpdateState object:nil];
});
});
}
更新
如果我在一个带有微调器的警报视图中添加如下代码,我会在异步队列中调用它还是在实例化myQueue之前
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:@"Please wait\n\n\n"
preferredStyle:UIAlertControllerStyleAlert];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(130.5, 65.5);
spinner.color = [UIColor blackColor];
[spinner startAnimating];
[alert.view addSubview:spinner];
[self presentViewController:alert animated:NO completion:nil];
然后我可以使用:
关闭get_main_queue中的警报视图[alert dismissViewControllerAnimated:YES completion:nil];