2011-12-02 1 views
1

ASIHTTPRequest를 사용하여 iOS 용 앱을 만들려고하는데 사용하는 데 문제가 있습니다. 내 문제를 설명하기 위해 테스트 프로젝트를 업로드했습니다.이 프로젝트는 http://uploads.demaweb.dk/ASIHTTPRequestTester.zip에서 다운로드 할 수 있습니다. 동기 방법은 잘 작동되지만,ASIHTTPRequestTester : 비동기가 작동하지 않습니다.

#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 

비동기가 전혀 작동하지 않습니다 :

나는 ASIHTTPRequestDelegate 프로토콜을 사용하십시오 WebService 클래스를 만들었습니다. 먼저 requestFinished와 requestFailed는 호출되지 않았고 지금은 EXC_BAD_ACCESS를 얻고 있습니다. 두 가지 테스트 메소드는 내 ViewController의 viewDidLoad에서 호출됩니다. 누군가가 나를 도와 줄 수 있기를 바랍니다.

EDIT1 : this topic 당으로 , 그것 때문에 내 프로젝트를 사용할 수있는 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 

답변

3

당신은 (당신의 뷰 컨트롤러를 말한다)가 그 일을 한 후, 다음합니다 (WebService 제거하는 클래스를 말할만큼 오래 머물 수 개체에 대한 강한 참조로 WebService 인스턴스를 추가 할 수 있습니다 예를 들어, requestFinishedrequestFailed이면 WebService 인스턴스를 해제하라는 메시지가 표시되면서보기 컨트롤러로 다시 호출됩니다.

+0

답변 해 주셔서 감사합니다. ViewController에 강력한 속성을 추가 한 내'edit2'를 보시기 바랍니다. 잘 작동하지만 더 나은 방법이 있습니까? – dhrm

0

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]; 
관련 문제