两部分问题
Part one:个 我正在try 创建对数据库的异步请求.我目前正在同步做这件事,但是我想同时学习这两种方法,以更好地理解正在发生的事情.
目前,我已经像这样设置了我的同步呼叫.
- (IBAction)setRequestString:(NSString *)string
{
//Set database address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://127.0.0.1:8778/instacodeData/"]; // imac development
//PHP file name is being set from the parent view
[databaseURL appendString:string];
//call ASIHTTP delegates (Used to connect to database)
NSURL *url = [NSURL URLWithString:databaseURL];
//SynchronousRequest to grab the data
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *error;
NSURLResponse *response;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!result) {
//Display error message here
NSLog(@"Error");
} else {
//TODO: set up stuff that needs to work on the data here.
NSString* newStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(@"%@", newStr);
}
}
我想我需要做的是更换电话
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
使用异步版本
sendAsynchronousRequest:queue:completionHandler:
但是我不确定该传递给队列或completionHandler什么...如有任何示例/解决方案,将不胜感激.
Part two: 我一直在阅读有关多任务处理的文章,我想通过确保我的连接请求在出现中断时完成来支持它.我一直在关注这example
它解释了在中断发生时如何争取更多的时间,我知道它在做什么.但不知道如何将其应用于此连接?如果您有任何示例/教程来帮助我弄清楚如何应用它,那将是非常棒的!