ASIHTTPRequestTester: asynchronous does not work

I am trying to make an iOS app using ASIHTTPRequest, but before that I am facing some problems. To demonstrate my problems, I uploaded a test project, which you can download here: http://uploads.demaweb.dk/ASIHTTPRequestTester.zip .

I created a WebService class that uses the protocol ASIHTTPRequestDelegate:

#import "WebService.h"
#import "ASIHTTPRequest.h"

@implementation WebService

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSLog(@"requestFinished");
}
- (void)requestFailed:(ASIHTTPRequest *)request {
    NSLog(@"requestFailed");
}

- (void) testSynchronous {
    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    NSLog(@"starting Synchronous");
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        NSLog(@"got response");
    }
}

- (void) testAsynchronous {
    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];

    NSLog(@"starting Asynchronous");
    [request startAsynchronous];
}

@end

The synchronous method works fine, but the asynchronous method doesn't work at all. At first, requestFinished and requestFailed were never called, and right now I am getting EXC_BAD_ACCESS. Two test methods are called from my ViewController viewDidLoad. Hope someone can help me do this work.

EDIT1: , - Automatic Reference Counting, . [self retain], ARC. - ?

EDIT2: MrMage.

@interface ViewController()
@property (nonatomic,strong) WebService *ws;
@end

@implementation ViewController

@synthesize ws = _ws;

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setWs:[[WebService alloc] init]];
    [self.ws testSynchronous];
    [self.ws testAsynchronous];
}

@end
+1
2

WebService , (, ), , WebService , ( .. requestFinished requestFailed , WebService).

+3

ASIHTTPRequest ARC.

.

NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];

//set request like this to avoid retain cycle on blocks
__weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

//if request succeeded
[request setCompletionBlock:^{        
    [self requestFinished:request];
}];

//if request failed
[request setFailedBlock:^{        
    [self requestFailed:request];
}];

[request startAsynchronous];
0

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


All Articles