【发布时间】:2014-06-19 22:53:13
【问题描述】:
我正在使用 AFNetworking (2.3.1) 解析 JSON 数据并将其显示在标签中。
为此,我使用setCompletionBlockWithSuccess,它在AFHTTPRequestOperation.h 中声明。
在viewDidLoad 上调用了三个这样的函数,一个看起来像:
-(void)parse {
NSURL *url = [[NSURL alloc] initWithString:kURL];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Parse Successful");
//Code for JSON Parameters and to display data
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", [error localizedDescription]);
//Code for Failure Handling
}];
[operation start];
}
虽然这就像一个魅力,因为它包含在一个块请求中,这个过程会在整个应用程序状态中继续进行。因此,当不需要显示此数据时,请求仍在加载,并且由于这些块,我收到了内存警告。
我的问题是,一旦我离开创建它们的 View Controller 以节省内存和数据,或适当地处理它们,如何停止、取消或暂停这些进程?
如果这是一个明显的答案,请原谅我,我只是以完全错误的方式处理或创建块。我对 AFNetworking 和块、操作、异步请求等都是新手。
谢谢。
【问题讨论】:
标签: ios objective-c afnetworking objective-c-blocks nsoperation