1

我已经实现了一个类,我在其中封装了一个异步请求NSURLConnection及其委托方法。每当点击视图的按钮时,我都会在视图控制器中创建此类的一个实例,并要求它发出网络请求:

- (IBAction)getData:(id)sender
{
   // Perform network request
   Updater *updater = [[Updater alloc] init];
   [updater queryService:self.date];
}

这样queryService:的方法是这样的:

- (void)queryService:(NSDate *)date
{
   self.responseData = [NSMutableData data];

   NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kTimeout];
   NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
}

由于Updater正在执行异步操作,我不确定updater我声明为局部变量的实例是否会保留到connection:didFailWithError:或被connectionDidFinishLoading:调用,或者我会在调用视图控制器中为strong. Updater我正在使用ARC。

谢谢!

4

2 回答 2

2

Yes, it will be retained until the connection ends (fail, success ecc). It happens because your Updater instance is the delegate of NSURLConnection.

Inside NSURLConnection doc you can read:

Note: During a download the connection maintains a strong reference to the delegate. It releases that strong reference when the connection finishes loading, fails, or is canceled.

于 2013-07-14T19:41:06.380 回答
-1

You need a strong reference, could be' an ivar to Update or an ivar to a collection with update instances. Even if the delegate is retained you did not explain how you get data back.

于 2013-07-14T19:41:41.090 回答