objective-c - 在 Objective-C 中使用嵌套block : EXC_BAD_ACCESS

标签 objective-c cocoa-touch uikit objective-c-blocks twrequest

使用 iOS 5 的新 TWRequest API,我遇到了与 block 使用相关的问题。

我需要做的是在收到对第一个请求的成功响应后,立即触发另一个请求。在第二个请求的完成 block 中,我会通知多步操作成功或失败。

我大致是这样的:

- (void)doRequests
{
    TWRequest* firstRequest = [self createFirstRequest];
    [firstRequest performRequestWithHandler:^(NSData* responseData,
                                              NSHTTPURLResponse* response,
                                              NSError* error) {
        // Error handling hidden for the sake of brevity...
        TWRequest* secondRequest = [self createSecondRequest];
        [secondRequest performRequestWithHandler:^(NSData* a,
                                                   NSHTTPURLResponse* b,
                                                   NSError* c) {
            // Notify of success or failure - never reaches this far
        }];
    }];
}

我不会保留任何请求或在任何地方保留对它们的引用;它只是即发即弃。

但是,当我运行该应用程序时,它因 EXC_BAD_ACCESS 而崩溃:

[secondRequest performRequestWithHandler:...];

它很好地执行了第一个请求,但是当我尝试使用处理程序启动第二个请求时,它崩溃了。该代码有什么问题?


创建请求的方法非常简单:

- (TWRequest*)createFirstRequest
{
    NSString* target   = @"https://api.twitter.com/1/statuses/home_timeline.json";
    NSURL* url         = [NSURL URLWithString:target];
    TWRequest* request = [[TWRequest alloc]
                          initWithURL:url parameters:params
                          requestMethod:TWRequestMethodGET];
    // _twitterAccount is the backing ivar for property 'twitterAccount',
    // a strong & nonatomic property of type ACAccount*
    request.account    = _twitterAccount;

    return request;
}

最佳答案

确保您保留一个引用/保留拥有您用于签署 TWRequestACAccountACAccountStore

如果您不这样做,ACAccount 将变得无效,然后您在尝试触发使用它签名的 TWRequest 时将获得 EXC_BAD_ACCESS。

关于objective-c - 在 Objective-C 中使用嵌套block : EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8662156/

相关文章:

ios - dequeueReusableCellWithIdentifier 创建重复的 UITableviewcell

iphone - 签名仅对临时无效

cocoa-touch - 不使用 transitionFromViewController 时我必须发送的 UIViewController 包含消息的正确顺序是什么?

cocoa - 如何以视觉格式设置自动布局默认间距指标的优先级?

ios - UIImageView 和自动布局

ios - 从 UIImagePickerController 获取完整图像

objective-c - 在 Xcode 中, View 或窗口以何种方式知道 mouseDown 在其自身之外?

swift - UIButton 在滚动时丢失 cornerRadius(设置其他属性时)

objective-c - 如何使用 Objective-C 解析 JSON?

objective-c - 如何检查 NSURL 是否包含 NSString null?