Grand Central Dispatch (GCD) 和异步 API

发布于 2024-10-04 07:29:37 字数 988 浏览 0 评论 0原文

我正在使用 Twitter API 来发布推文。有时这可能需要一些时间,所以我想在后台执行“推文发布”操作。为此,我正在使用 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
    });
}

问题是,我没有收到委托回调!我缺少什么?

I'm using Twitter API to post tweets. At times this can take some time, so i want to perform the "Tweet posting" operation in the background. For that i'm using GCD, like this:

- (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
    });
}

The problem is, I'm not getting the delegate callback! What am i missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶茶白久 2024-10-11 07:29:37

您是否尝试在主线程上运行 Twitter 连接?如果它在主线程上运行而不是在某些后台队列上运行,则您可能会遇到 NSURLConnection 的运行循环问题。 (只是一个疯狂的猜测。)

Did you try running the Twitter connection on the main thread? If it works on the main thread and not on some background queue, you might have run into run loop issues with NSURLConnection. (Just a wild guess.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文