0

I have a 2 tab iPhone application: the first one downloads images from a web server, parses the data, and then displays it in a CollectionView. Obviously I want to do all the work in the background, and update the UI in the main thread when this is done. The problem is, when I switch to the Camera (Tab #2), I have a black screen preview. I used instruments, read all the threads on SO about this problem, and still can't get it to work. The ONLY way I successfully and permanently solved the problem is to use only the main thread in tab #1 to download, then parse then update UI. Somebody has a clue on what to do here?

This randomly blocks camera:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

NSOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(initialPostLoading) object:@"Op1"];
NSOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadSomeBrands) object:@"Op2"];

NSOperation *completion = [NSBlockOperation blockOperationWithBlock:^{

    NSLog(@"2");

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{


        [self.collectionView reloadData];
        [HUD show:NO];
        [HUD setHidden:YES];
        [refreshControl endRefreshing];
        NSLog(@"Back in Main Thread");

    }];
}];


[completion addDependency:op1];
[completion addDependency:op2];

[queue addOperations:@[op1, op2, completion] waitUntilFinished:NO];


NSLog(@"3");

This doesn't block camera:

[self initialPostLoading];
[self downloadSomeBrands];

[self.collectionView reloadData];
anthoprotic
  • 827
  • 2
  • 13
  • 24
  • Hi @mvb, I used Rob's example in this discussion for my app: http://stackoverflow.com/questions/19284249/multiple-nsthreads-synchronization – anthoprotic Mar 12 '14 at 17:40
  • I don't have a definite answer with the given information and code. But check [NSOperationQueue Class Reference](https://developer.apple.com/library/mac/documentation/cocoa/reference/NSOperationQueue_class/Reference/Reference.html) section "Getting specific operation queues". I think you must alloc a queue and call these operations on them. – mvb Mar 12 '14 at 17:48

0 Answers0