I'm unable to find error in this code:
-(void)downloadImageFromURL:(NSURL*)url withCompletionBlock:(RSSMessageImageDownloadCompletionBlock)completionBlock
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
__block RSSMessage *_self = self;
request.completionBlock =
^{
__block NSData *responseData = request.responseData;
dispatch_async( dispatch_get_main_queue(), ^{
_self.image = responseData;
[[[UIApplication sharedApplication] delegate] saveContext];
if(completionBlock != nil)
{
completionBlock();
}
});
};
[request startAsynchronous];
}
In this form I've got a memory leak from instruments. I was assuming it is because I've lacked the __block keyword before: ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
But when I've added this keyword to above line I've got error like:
* -[NSConcreteMutableData isNSData__]: message sent to deallocated instance 0xdeab380
I do not know how to retain the request data and not leak memory.
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}];
[request setFailedBlock:^{
NSError *error = [request error];
}];
[request startAsynchronous];
}
seems like the request itself needs to be marked with __block
Note, that the original author of ASIHTTPRequest isnt supporting it anymore. He explains his reasons very well and give links and suggestions for alternative projects. I am happy with AFNetworking, that has a great block based interface.