ASIHTTPRequest作为实例变量并解除分配和释放

时间:2011-11-11 12:11:33

标签: iphone asihttprequest nsoperation

    "Informazioni.h file"
@interface Informazioni : UIViewController{
            .....
 ASIHTTRequest *mASIHTTPRequest;
}
@property (nonatomic, retain) ASIHTTRequest *mASIHTTPRequest;
----------------------------
#import "Informazioni.h"
#import "Globals.h"

@implementation Informazioni

@synthesize mImageViewImmagine;
            ....

@synthesize mASIHTTPRequest;


- (void)viewDidLoad {
[super viewDidLoad];
//start fetching based on id_prodotti
[self startFetch:mId_prodotto];
}


- (void) startFetch:(NSString *) pId_prodotto{
    //activate ASIHTTPDownloadCache

    NSURL *url = [NSURL URLWithString:[JSON_DESCRIZIONE stringByAppendingString:mId_prodotto]];//JSON_DATA

    mASIHTTPRequest = [ASIHTTPRequest requestWithURL:url];
    [mASIHTTPRequest setDelegate:self];
    [mASIHTTPRequest startAsynchronous];

}


- (void)loadDataWithOperation: (NSString *) responseString{
    NSLog(@"load data with operation");

    NSDictionary *tempDict = [[responseString JSONValue] objectForKey:@"descrizione_prodotto"];

    NSLog(@"descrizione_prodotto: %@",tempDict);



    [self.mTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
    NSLog(@"reloadData called");


}



//start
- (void)requestFinished:(ASIHTTPRequest *)request{
    NSLog(@"SUCCESS Http fetching");

    // Operation Queue init (autorelease) 
    NSOperationQueue *queue = [NSOperationQueue new];
    // Create our NSInvocationOperation to call loadDataWithOperation, passing in nil
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                            selector:@selector(loadDataWithOperation:)
                                                                              object:[request responseString]];
    // Add the operation to the queue
    [queue addOperation:operation];
    [operation release];


}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",[error localizedDescription]);
    /*
     NSLog(@"Error: %@",[error localizedDescription]);
     UIAlertView *alert = [[UIAlertView alloc] 
     initWithTitle:@"DIUNAMAISHOP" 
     message:[error localizedDescription] 
     delegate:self 
     cancelButtonTitle:@"OK" 
     otherButtonTitles:nil];
     [alert show];
     [alert release];
     */
    /*
     //remove activity indicator
     if (self.mActivityIndicator.mFlag == YES) {
     [self.mActivityIndicator.view 
     performSelectorOnMainThread:@selector(removeFromSuperview) 
     withObject:nil waitUntilDone:YES];
     }
     */


}

-(void) queueFinished:(ASIHTTPRequest *) queue{
    //You could release the queue here if you wanted
    NSLog(@"Queue finished");
}
// end 

               ........

- (void)dealloc {
    //safely dealllocate

    [mASIHTTPRequest clearDelegatesAndCancel];
    [mASIHTTPRequest release];
              .....     
    [super dealloc];
    NSLog(@"Informazioni deallocated");
}


@end

我只是按下这个视图然后按下将dealloc /释放viewcontroller .. - 问题是当我在取回时按下它时会崩溃 - 我怎么能克服这个任何建议会做tnx

1 个答案:

答案 0 :(得分:0)

    mASIHTTPRequest = [ASIHTTPRequest requestWithURL:url];

您没有保留此请求。您需要保留它以获得有效的参考引用,然后才能取消并释放它。添加保留,或使用self.mASIHTTPRequest。