2011-01-25 64 views
3

我有一個問題,當我有一個掛起的ASIFormDataRequest(作爲異步任務啓動),仍在執行和用戶按下後退按鈕(爲了彈出視圖)我的viewController。Iphone停止ASIFormDataRequest

有什麼辦法可以阻止異步任務嗎?

我讀過這是一個名爲「clearDelegatesAndCancel」的方法,但我不知道它是我在找什麼。

感謝

+0

clearDelegatesAndCancel是你正在尋找的。 – KishoreK 2011-01-25 15:32:19

回答

6

事情是,打電話給clearDelegatesAndCancel,你必須有一個處理異步運行的ASIFromDataRequest對象。這意味着你應該將其設置爲一個屬性,就像...

@interface MyViewController : UIViewController <ASIHTTPRequestDelegate> 
{ 
    ASIFormDataRequest *theRequest 
    ... 
} 

@property (nonatomic, retain) ASIFormDataRequest *theRequest; 

然後在您的m,不申報新的請求對象,只是你formdatarequest定義的類的實例變量:

@synthesize theRequest; 

-(void)viewDidLoad //or whatever 
{ 
    self.theRequest = [ASIFormDataRequest requestWithUrl:myUrl]; 
    // then configure and fire the request, being sure to set .delegate to self 
} 

-(void)viewWillDisappear:(BOOL)animated //or whatever 
{ 
    [self.theRequest clearDelegatesAndCancel]; 
} 

-(void)dealloc 
{ 
    [theRequest release]; //don't not do this. 
} 

需要指出的是,您需要設置自己,以便在異步運行時請求對話。

順便說一下,這是非常好的做法。如果你的viewcontroller在你的請求返回之前消失了(比如通過彈出UINavController棧),它會嘗試調用一個釋放對象上的委託方法,並繁榮。

3

從ASI文檔(http://allseeing-i.com/ASIHTTPRequest/How-to-use)

 
    To cancel an asynchronous request (either a request that was started with 
    [request startAsynchronous] or a request running in a queue you created), 
    call [request cancel]. Note that you cannot cancel a synchronous request. 

    Note that when you cancel a request, the request will treat that as an error, 
    and will call your delegate and/or queue’s failure delegate method. If you do 
    not want this behaviour, set your delegate to nil before calling cancel, or 
    use the clearDelegatesAndCancel method instead.