任何想法,爲什麼sendSynchronousRequest
如下造成泄漏?儀器聲明負責的框架是NSURLConnection
,它指向NSCFString
與sendSynchronousRequest
排隊泄漏。泄漏報告問題與NSURLConnection的sendSynchronousRequest
我讀過,這是一個已知的問題OS 2.2或以前的東西,但絕對應該已經得到解決。有什麼想法嗎?
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/api/v1/dosomething"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0];
[request setHTTPMethod:@"POST"];
NSData *bodyData;
[request setValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
NSString *body = @"test";
bodyData = [body dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
[body release];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
無論如何,使用同步連接是非常可怕的。您在運行可能較長的網絡操作時阻止主線程。 –
邁克說的通常是很好的建議,但只是爲了細化它 - 你阻止了調用線程,這可能不是主線程。 – nall
@Mike感謝您的提示,但我完全意識到,這當然不是「主」線程。這是一個後臺線程,實際上我需要一個同步結果來繼續這個線程。 – marcc