Question

I have some code I want to add to an operation queue, the problem is the code functions when its not in the queue, but once added to the queue nothing happens.

Here's the code I want to add to the queue:

NSString* graphRequest = @"https://graph.facebook.com/redacted/picture?type=square";
FBRequest *fbRequest = [FBRequest requestForGraphPath: graphRequest];
[fbRequest startWithCompletionHandler:
 ^(FBRequestConnection *connection, id result, NSError *theError)
 {
     NSLog(@"Completed");
 }];

When the code above is executed then its completion block is invoked in a couple of seconds or so.

However if I try to execute the same code in an operation queue then the completion block is never invoked:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:
 ^{
    NSString* graphRequest = @"https://graph.facebook.com/redacted/picture?type=square";
    FBRequest *fbRequest = [FBRequest requestForGraphPath: graphRequest];
    [fbRequest startWithCompletionHandler:
     ^(FBRequestConnection *connection, id result, NSError *theError)
     {
         NSLog(@"Completed");
     }];
}];

With this code nothing happens.

Was it helpful?

Solution

The problem is that FBRequest will only work on the main thread for some reason. You really should not worry too much about it, since startWithCompletionHandler runs async anyway. If you really want to use NSOperationQueue you will have to to a dispatch_async to the main thread.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top