代码之家  ›  专栏  ›  技术社区  ›  Mustafa

Grand Central Dispatch(GCD)和异步API

  •  1
  • Mustafa  · 技术社区  · 15 年前

    我正在使用Twitter API发布tweets。有时这需要一些时间,所以我想在后台执行“Tweet posting”操作。为此,我使用GCD,如下所示:

    - (void)myClassMethodToPostTweet {
        dispatch_async(network_queue, ^{
            // … construct the tweet message
            NSString *tweet = @"…";
    
            // … check if network is available
            [self isConnectedToWeb];
    
            // … initialize twitter API
            TwitterAPIClass *twitterAPI = [[[TwitterAPIClass alloc] init…] autorelease];
            twitterAPI.delegate = self;
            twitterAPI.APIKey = ...;
            twitterAPI.APISecret = ...;
    
            // … use twitter API to post the tweet
            [twitterAPI postTweet:tweet];
        });
    }
    
    ...
    /* and when the API reports a successful operation, update the required variables and UI */
    ...
    
    - (void)twitterAPIDelegateMethodReportingOperationSuccess {
        // … update any variables/records
    
        // … update UI
        dispatch_async(dispatch_get_main_queue(), ^{
            // … UI updation code
        });
    }
    

    问题是,我没有收到委托回调!我错过了什么?

    1 回复  |  直到 14 年前
        1
  •  2
  •   zoul    15 年前

    你试过在主线程上运行Twitter连接吗?如果它在主线程上工作,而不是在某个后台队列上,则可能遇到了 NSURLConnection . (只是胡乱猜测。)