dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:CONNECTIONT_TIMEOUT]; NSString *encodedStr = [body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSData *sendData = [encodedStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; NSString *dataLengthStr =[NSString stringWithFormat:@"%lu",(unsigned long)[sendData length]]; [request setValue:dataLengthStr forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:sendData]; self.connection = [NSURLConnection connectionWithRequest:request delegate:self]; });
这是一个在主线程中执行的代码,执行结果有问题,无法收到网络回掉。
分析待续。。。
需要研究为什么connection不能阻止线程结束!什么东西能阻止??
时间: 2024-11-25 13:53:19