I have a tab-based app with 2 tabs: the first one performs multiple operations in a background thread (downloading json) and updates the UI on the main thread when the fetching is over. The second tab presents a camera as soon as it appears. When I open the app, the fetching starts in background in tab #1. If I switch to tab #2 while in the background thread in tab 1, the camera loads. If I wait until the main thread updated the UI (still tab 1) before switching to tab 2, the camera takes 10 seconds to load, only showing a black screen. What's even more weird is that the NSLogs tell me the camera is supposed to be already loaded, but a black screen shows up. My question is, is there a way to "clear" the main thread when tab #2 appears, or even better, is there a way to show the camera as a high priority task in the main thread?
This is the code in ViewDidAppear (Tab 2):
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"4");
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
});
Next:
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
NSLog(@"5");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"6");
[[UIApplication sharedApplication] setStatusBarHidden:YES];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
NSLog(@"7");
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.delegate = self;
imagePickerController.showsCameraControls = NO;
// NSLog(@"HERE");
if (isiPhone5)
{
NSLog(@"8");
[[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
}
else
{
NSLog(@"not 5");
[[NSBundle mainBundle] loadNibNamed:@"Over2" owner:self options:nil];
}
self.overlayView.frame = imagePickerController.cameraOverlayView.frame;
imagePickerController.cameraOverlayView = self.overlayView;
self.overlayView = nil;
self.imagePickerController = imagePickerController;
[self presentViewController:self.imagePickerController animated:NO completion:nil];
NSLog(@"9 DONE");
}
});
}