在Objective-C中使用块内的块:EXC_BAD_ACCESS

bia*_*bit 6 cocoa-touch objective-c uikit objective-c-blocks twrequest

使用iOS 5的新TWRequestAPI,我遇到了与块使用相关的砖墙.

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

这大致是我正在做的事情:

- (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
        }];
    }];
}
Run Code Online (Sandbox Code Playgroud)

我没有保留任何一个请求或在任何地方保留对它们的引用; 这只是一场千载难关.

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

[secondRequest performRequestWithHandler:...];
Run Code Online (Sandbox Code Playgroud)

它执行第一个请求就好了,但是当我尝试使用处理程序启动第二个请求时,它会崩溃.这段代码出了什么问题?


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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

Fre*_*ira 18

确保你保留一个参考/保留你用来签署s的ACAccountStore所有权.ACAccountTWRequest

如果你不这样做,那么ACAccount它将变为无效,然后当你尝试TWRequest使用它签名时你将得到EXC_BAD_ACCESS .