希望这是一个合适的问题。我的目标是
1.add a controller into an array name 'arrControllers'
2.access and get current controller from 'arrControllers' to do sth with it
我只是想确保arrControllers
在我访问和使用它之前应该准备好。这就是我使用serial queue
并dispatch_sycn
喜欢以下内容的原因。
-(void)viewDidLoad
{
[super viewDidLoad];
firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self setUpWithView:firstViewController];
}
并且setUpWithView:
是
-(void)setUpWithView:(UIViewController*)viewController {
dispatch_queue_t queue;
queue = dispatch_queue_create("my queue", NULL);
containerController = [ContainerViewController sharedContainerController];
// What I am taking about is from now on
dispatch_sync(queue, ^{
[containerController initWithRootViewController:viewController];
});
dispatch_sync(queue, ^{
[containerController setUpView];
});
}
并且initWithRootViewController:
是
- (void)initWithRootViewController:(UIViewController*)rootViewController {
arrControllers = [[NSMutableArray alloc] init];
arrControllers = [NSMutableArray arrayWithObject:rootViewController];
}
并且setUpView
是
-(void)setUpView {
/* Process to ADD currentController TO hierarchy */
[self addChildViewController:[arrControllers lastObject]];
............................................................
}
据我所知,编译器将逐行执行代码,这意味着通过执行以下操作
-(void)setUpWithView:(UIViewController*)viewController {
containerController = [ContainerViewController sharedContainerController];
// Not using serial_queue and dispatch_sync
[containerController initWithRootViewController:viewController];// line1
[containerController setUpView]; //line2
}
编译器将执行第 2 行,直到它完成第 1 行的任务。
我的问题是
1. is it necessary to using `serial_queue` and `dispatch_sycn`for this situation.
PS:如果您认为这是一个不好的问题。请评论您对此的看法。