我有一个类可以处理iPhone上的所有网络访问。我用以下方式使用它:
-(void) randomFunction:
{
// Assign params etc. <br />
TTNetworkAccessLib *nLib = [[TTNetworkAccessLib alloc] initWithParams:..];
[nLib createAndStartRequest];
// Can't release here since the request is async and is still running.
}
-(void) requestSucceded:(NSData *) data
{
// Can pass a reference to the request object and release here, but doesn't seem right?
}
-(void) requestFailed:(int) responseCode
{
// Can pass a reference to the request object and release here, but doesn't seem right?
}
答案 0 :(得分:2)
当我创建一个对象以异步方式执行某些操作时,我通常会在启动过程时保留它自己并在完成和完成任何回调后释放自己。这样,创建它的代码可以在以后不需要的时候释放它,并且我知道在该过程完成之前它不会被释放。
答案 1 :(得分:0)
有了这样的代表,我建议使用autorelease TNetworkAccessLib * nLib = [[[[TTNetworkAccessLib alloc] initWithParams:..] autorelease];