我目前正在研究一個應用程序,我需要接收數據以便其非常重要,所以不用異步使用同步。然而,這引起了一個非常不幸的副作用,同步請求鎖定了UI線程。ASIHTTPRequest同步請求崩潰的應用程序
我爲解決這個問題所做的工作是將Multithreading引入到我的應用程序中,使用拯救生命的「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
希望,讓你的什麼即時試圖做一個更好的主意,我想,也許我失去了一些東西與我聲明我syncronious啓動方式..作爲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 ..等我不知道如果有人可以幫助我,這將是偉大的。
你不會有在iOS與ASIHTTPRequest多少運氣 - 誰創造和維護它已放棄了它的開發者。將不會有ARC支持,Apple不會改進網絡堆棧。我強烈建議您使用內置網絡支持或其他第三方庫。 – RyanR 2012-02-08 03:46:48
爲什麼不直接排列你的請求,如果它真的很重要的數據按順序? 'ASIHTTPRequest'支持'NSOperationQueue'(和'ASINetworkQueue'),你可以將'maximumConcurrentOperationCount'設置爲1並按順序排列所有的請求。 – 2012-02-08 03:48:59
DAMNIT!我愛ASI ..爲了得到結果我需要一隻猴子的兒子,我現在必須學習如此多的廢話!在這種情況下,我應該考慮離開ASI應考慮哪些類型的事情? – 2012-02-08 04:08:14