我目前正在处理一个我需要接收数据的应用程序,因为它非常重要,所以不使用异步我使用同步。然而,这引入了非常不幸的副作用,同步请求会锁定UI线程。
我正在采取的措施是使用拯救生命的“Grand Central Dispatch”服务将多线程引入我的应用程序,到目前为止这似乎很容易让我理解。
所以考虑到这一切,我遇到了我正在做的事情的问题,以前我使用异步,一切都很好,改变为同步给我这个错误
Error Domain=ASIHTTPRequestErrorDomain Code=1 "A connection failure occurred" UserInfo=0x68052a0 {NSUnderlyingError=0x683d250 "The operation couldn’t be completed. Connection refused", NSLocalizedDescription=A connection failure occurred}
到目前为止我的代码。
- (IBAction)setRequestString:(NSString *)string
{
//Set database address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://192.168.1.1:8778/Data/"]; // iphone 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];
//Used to Check which ASI cache to use (also used in requestFinished
xmlFileName = [[NSString alloc] initWithFormat:string];
//Set up multithread with GCD
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
//Create If statments here to set up the different caches to be passed down to the next view
if ([string isEqualToString:@"high.xml"]){
//Cache stuff goes in here
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days - this will change to cache until DBVersion changes
[request setDelegate:self]; // this calls the delegate function requestFinished
dispatch_sync(queue, ^ {
[request startSynchronous];
});
}else if ([string isEqualToString:@"low.xml"]){
//Cache stuff goes in here
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days - this will change to cache until DBVersion changes
[request setDelegate:self]; // this calls the delegate function requestFinished
dispatch_sync(queue, ^ {
[request startSynchronous];
});
}
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
//.... etc
希望这可以让你更好地了解我想要做什么,我想也许我错过了我宣布同步开始的方式..就像在asihttprequest帮助文件中他们说的那样声明它
- (IBAction)grabURL:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
}
然而我正在使用数据..所以这一行
NSString *response = [request responseString];
不行吗?剂量它需要NSData ..等我不知道如果有人可以帮助我,这将是伟大的。
答案 0 :(得分:0)
你可以使用nsoperationqueue ...... 您可以创建一个NSoperationInvoke并按顺序将它们添加到NSoperationQueue(在发送另一个请求的数据后)...您可以将观察者添加到NSOperationQueue以确保一次处理多少请求...在您的情况下它将只是一个......在观察者收到同步过程完成后的通知后,它将通过performonMainThread调用一个选择器来启动观察者本身的另一个请求......
on NSString *response = [request responseString];
问题,您可以[request iskindofClass:[ClassName class]];
检查请求对象
或nslog(“%@”,[请求描述]);
它将告诉哪种对象请求
答案 1 :(得分:0)
您是否考虑过在代码中添加一个串行队列?
dispatch_queue_t queue = dispatch_queue_create("com.myapp", NULL);
您正在使用并发线程,这导致多个操作同时发生。另外,在队列块中正确包装ASIHttpRequest代码。
尝试一下,让我们知道