【问题标题】:ASIHTTPRequest as instance variable and deallocating and releasingASIHTTPRequest 作为实例变量并解除分配和释放
【发布时间】:2011-11-11 12:11:33
【问题描述】:
    "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

我只是简单地推动了这个视图,然后按回将释放/释放视图控制器.. - 问题是当我在获取时按下回它会崩溃 - 我怎样才能克服这个任何建议都会做tnx

【问题讨论】:

    标签: iphone asihttprequest nsoperation


    【解决方案1】:
        mASIHTTPRequest = [ASIHTTPRequest requestWithURL:url];
    

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

    【讨论】:

    • 有点像这样? self.mASIHTTPRequest = [ASIHTTPRequest requestWithURL:url]; [self.mASIHTTPRequest setDelegate:self]; [self.mASIHTTPRequest startAsynchronous];dealloc 怎么样,我还应该添加 self.mASIHTTPRequest [self.mASIHTTPRequest clearDelegatesAndCancel]; [self.mASIHTTPRequest release]; tnx 以便快速回复,遗憾的是我直到星期一才能测试代码.. 一旦我测试过就会更新。
    • self.mASIHTTPRequest = [ASIHTTPRequest requestWithURL:url]; 会这样做;无需更改任何其他内容
    • 感谢它有效..!我认为没有self. 也可以工作,因为我认为没有它它们也可以工作。
    猜你喜欢
    • 1970-01-01
    • 2014-07-05
    • 2011-05-03
    • 2012-05-07
    • 2011-08-23
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多