【发布时间】:2012-02-13 17:07:17
【问题描述】:
我需要对某个 url 进行几次 https 调用。因此我做这样的事情
//ViewController Source
-(IBAction) updateButton_tapped {
[self performSelectorInBackground:@selector(updateStuff) withObject:nil];
}
-(void) updateStuff {
// do other stuff here...
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url]];
[request setHTTPMethod:@"POST"];
NSData *postData = [[Base64 encodeBase64WithData:payload] dataUsingEncoding:NSASCIIStringEncoding];
[request setHTTPBody:postData];
NSURLResponse* response = [[NSURLResponse alloc] init];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
//Process the recieved data...
//Setup another synchronous request
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
//Process data
//do this another 4 times (note for loop cannot be use in my case ;) )
//Finally update some view controllers
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationIdentifier" object:self];
}
因此,此代码的问题在于它随机崩溃(并非总是如此,而是经常崩溃)。我在日志上没有调试输出。有时我的整个应用程序冻结或它只是使整个程序崩溃。但如果我在主线程上运行它,它永远不会崩溃。因此我认为代码是正确的,我想现在它与 iphone 上的线程有关。
以这种方式运行代码会出现什么问题,什么可能导致随机崩溃?
【问题讨论】:
标签: ios multithreading httprequest