我需要帮助。我正在创建一些ASIHTTPRequests,但它有时会崩溃。
我发现,这是由于初始化造成的。我必须使用这样的代码:
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:myURL];
{my code}
[request release];或者这样:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:myURL];你能分辨出区别吗?
PS:我有十个请求,一次开始,还有一个是之前开始的。
发布于 2011-12-13 21:35:33
第一个是标准分配,第二个是返回autoreleased对象的类方法。
假设您在控制器(或viewController)中有此代码,则request对象可能会在方法结束时被释放。所以你需要在你的控制器中保留一个对该对象的引用,这样它就不会被释放。
因此,您的接口定义:
@interface MyController : UIViewController {
ASIHTTPRequest *request;
// ...
}
@property (nonatomic, retain) ASIHTTPRequest *request;
@end然后是实现
@implementation MyController
@sinthetyze request;
- (void) aMethodThatInstantiatesYourRequest { // using self.request means that we use the accessory method, which involves a retain call
self.request = [[ASIHTTPRequest alloc] initWithURL:myURL];
// ...
[request release];
// or using the autoreleased object
self.request = [ASIHTTPRequest requestWithURL:myURL];
}
- (void) dealloc { // don't forget to release the request object
[request release];
[super release];
}顺便说一句,如果您有多个请求,最好使用网络队列,因此您保留对队列的引用,而不是对各种单个请求的引用。
使用ASINetworkQueue的See this gist。
发布于 2012-01-29 15:41:42
您可以在查看源代码后发现差异。
第一部分:需要手动释放请求对象,但这将比第二种类型更稳定。
第二部分:使用autorelease方法创建请求。这在大多数情况下都能正常工作。
如果您应用程序需要处理繁重的请求分配和释放,强烈建议使用第一种方法来创建asi请求。
https://stackoverflow.com/questions/8489945
复制相似问题