NSURLSessionDataTask内存泄漏

时间:2016-08-05 10:02:16

标签: ios objective-c memory-leaks nsurlsession nsurlsessiondatatask

我试图用NSURLSessionDataTask做一些简单的事情来从服务器获取一些数据。以前我使用NSURLConnection来获取数据,但是想将我的代码移动到NSURLSessionDataTask。所以这里是代码

 NSString *_urlString = [NSString stringWithFormat:@"%@/integration/admin/token",BaseURL];

NSMutableURLRequest *_request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_urlString]];

[_request setHTTPMethod:@"POST"];
[_request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSData *_sendingData = [NSJSONSerialization dataWithJSONObject:@{@"username":@"demo", @"password":@"demo123"} options:0 error:NULL];

[_request setHTTPBody:_sendingData];

NSURLSessionConfiguration *_defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *_session = [NSURLSession sessionWithConfiguration:_defaultConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]];

NSURLSessionDataTask *_dataTask = [_session dataTaskWithRequest:_request completionHandler:^(NSData *_data, NSURLResponse *_response, NSError *_error)
                                   {
                                       if (_error)
                                       {
                                           NSLog(@"Error %@",[_error description]);
                                       }
                                       else
                                       {
                                           NSLog(@"No Error %@",[[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]);
                                       }
                                   }];

[_dataTask resume];
[_session finishTasksAndInvalidate];

完美无缺,我也得到输出。但是当我在Instrument中运行相同的代码时,我会发现以下泄漏Instrument Output

0 个答案:

没有答案