by this class I send http requests :
@interface AsyncRequest()
@property(strong,nonatomic)NSURLRequest* murlRequest;
@property(copy) OnCompleted onCompletedBlock;
@end
@implementation AsyncRequest
//Async Request object initialization
-(instancetype)initWithUrl:(NSString*)urlRequest onCompletedBlock:(OnCompleted) onCompeletedBlock
{
self = [super init];
if(self)
{
//blocks
NSURL* url = [NSURL URLWithString:[NSStringstringWithFormat:@"%@%@",SERVER_URL,urlRequest]];
self.onCompletedBlock = onCompeletedBlock;
self.murlRequest = [NSURLRequest requestWithURL:url];
}
return self;
}
//call this function to execute the specified request
-(void)ExecuteRequest
{
AsyncRequest * __weak weakself = self;
//localVariable is used by value, a strong reference is made to localVariable in the block
NSURLRequest *urlRequest = self.murlRequest;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^(void){
NSURLResponse *response = nil;
NSError *error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
//main ui
dispatch_async(dispatch_get_main_queue(),^{
weakself.onCompletedBlock(data);
});
}
@end
so I declare onComplete block and and initialize them at initWithUrlFunction then I use weakself to run It.
but the problem I get this error at weakself.onCompletedBlock(data) ->
Thread1:EXC_BAD_ACCESS(code=2,address=0xc)
what's the problem ?
sendAsynchronousRequest:queue:completionHandler:method, right?weakself.onCompletedBlockto a strong var first and test it is notnil