使用iOS 5的新TWRequest
API,我遇到了與塊使用相關的磚牆。在Objective-C的塊中使用塊:EXC_BAD_ACCESS
我需要做的是在收到第一個請求的成功響應後立即激發另一個請求。然後,在第二個請求的完成塊中,通知多步操作的成功或失敗。
這裏大概我在做什麼:
- (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;
}
請發佈'createFirstRequest'和'createSecondRequest'的代碼 – vikingosegundo 2011-12-28 22:53:13
您可以發佈'-createSecondRequest'的代碼嗎?我猜這個代碼在後臺線程上運行有問題,因爲從來沒有保證完成處理程序將運行在哪個線程上。 – 2011-12-28 22:54:27
完成;對不起'回合,並不認爲這是相關的。 – biasedbit 2011-12-28 23:00:36