Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting the NSURLConnection delegate callback into the right thread
    primarykey
    data
    text
    <p>I can't seem to get a Delegate callback to work when I thread my code. I am trying to call a remote server using NSURLConnection to get an access_token. The access_token is received in connection:didReceiveResponse: delegate. This is not a problem until I thread it. I thought I was passing the correct objects for my delegates, but it does not reach connection:didReceiveResponse: </p> <p>Can anyone see why connection:didReceiveResponse: in LoginViewController does not get called when I thread the call? Thanks.</p> <p>LoginViewController.m</p> <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSString *access_token = [self getTokenFromResponse: response]; [self.delegate didGetAccessToken:access_token]; } - (void)fetchAccessTokenNoUI { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"www.mysite.com"]]; [NSURLConnection connectionWithRequest:request delegate:self]; } </code></pre> <p>AccessToken.h</p> <pre><code>@interface AccessToken : NSObject @property (atomic, strong) LoginViewController *loginViewController; // doing this to try to keep it persistent - (void) fetchAccessTokenWithDelegate: (id &lt;LoginDelegate&gt;)delegate; @end </code></pre> <p>AccessToken.m</p> <pre><code>- (void) fetchAccessTokenWithDelegate: (id &lt; LoginDelegate &gt;)delegate { dispatch_queue_t downloadQueue = dispatch_queue_create("Fetch access_token queue", NULL); dispatch_async(downloadQueue, ^ { // this works fine if I don't do it in a queue self.loginViewController = [[LoginViewController alloc] init]; self.loginViewController.delegate = delegate; [self.loginViewController fetchAccessTokenNoUI]; }); dispatch_release(downloadQueue); } </code></pre> <p>CallingClass.m</p> <pre><code>- (void)didGetAccessToken:(NSString *)access_token { if (!access_token) { LoginViewController *userProfileViewController = [[LoginViewController alloc] init]; userProfileViewController.delegate = self; [self.navigationController pushViewController:userProfileViewController animated:YES]; } } - (IBAction)favourite:(id)sender { AccessToken *accessToken = [[AccessToken alloc] init]; [accessToken fetchAccessTokenWithDelegate:self]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload