Using blocks in blocks in Objective-C: EXC_BAD_ACCESS

Using the new TWRequest iOS 5 API, I came across a brick wall associated with using blocks.

What I need to do is get a successful response to the first request, immediately launch another. In the completion block of the second request, I notify of a successful or unsuccessful multi-step action.

Here's roughly what I am doing:

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

I do not save any of the requests or keep links to them anywhere; it's just fire and forget.

However, when I run the application, it is reset using EXC_BAD_ACCESS on:

 [secondRequest performRequestWithHandler:...]; 

It makes the first request just fine, but when I try to start the second with a handler, it will work. What is wrong with this code?


The ways to create queries are simple:

 - (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; } 
+6
source share
2 answers

Make sure you keep the link / save the ACAccountStore that owns the ACAccount that you use to sign TWRequest s.

If you do not, ACAccount will become invalid, and then you will get EXC_BAD_ACCESS when you try to run TWRequest with it.

+18
source

I am not familiar with TW *, so consider this a wild hunch ... try sending a block with a dedicated heap:

 [firstRequest performRequestWithHandler:[^ (NSData *responseData, ...) { ... } copy]]; 

To clarify, I think that the block you are sending is allocated in the form of a heap, so if TW * can save it, it will not matter if it has already left the area.

+1
source

Source: https://habr.com/ru/post/904639/


All Articles