2012-02-15 203 views
7

首先所有的問題都是故障碼simiple ..如果你只是想看看他們跳到這篇文章的底部,你會看到他們用粗體..更詳細的,你可以閱讀其餘的這篇文章...nsurlconnection異步請求

我只是想解決我的NSURLConnection,使其工作順利,我理解這一點。對於互聯網上異步連接的示例/教程缺乏深刻的理解,或者沒有我能夠發現的任何深層次的東西,除了讓連接啓動並運行之外,深入瞭解任何深度的事情,而且在處理它之後看起來很簡單。希望這個問題能夠填補我覺得在其他用戶身上存在的空白。

因此,在我的.h文件中,我已經導入了基礎標題並聲明瞭接收或缺少接收數據所需的方法(錯誤等)。

.H

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> //add foundations 
//.. other headers can be imported here 

@interface MyViewController: UITableViewController { 

//Im not setting any delegates to access the methods because Is all happening in the same 
//place so I just use the key word 'self' when accessing the methods declared below 
//I'm not sure if this is the best thing to do but I wasn't able to get my head around declaring the delegate or how it would help me with the way I have set up my request etc. 

} 
- (IBAction)setRequestString:(NSString *)string; //this method sets the request and connection methods 

//these methods receive the response from my async nsurlconnection 
- (void)receivedData:(NSData *)data; 
- (void)emptyReply; 
- (void)timedOut; 
- (void)downloadError:(NSError *)error; 

所以,這就是我的頭文件..很簡單,沒有太多的解釋需要。

.M

//call setRequestString from some other method attached to a button click or something 
[self setRequestString:@"rss.xml"]; 
//.. 

- (IBAction)setRequestString:(NSString *)string 
{ 
    //Set database address 
    NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http:www.comicbookresources/feeds/"]; // address not real jsut example 

    //append the string coming in to the end of the databaseURL 
    [databaseURL appendString:string]; 

    //prepare NSURL with newly created string 
    NSURL *url = [NSURL URLWithString:databaseURL]; 

    //AsynchronousRequest to grab the data 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
    { 
     if ([data length] > 0 && error == nil){ 
      [self receivedData:data]; 
     }else if ([data length] == 0 && error == nil){ 
      [self emptyReply]; 
     }else if (error != nil && error.code == NSURLErrorTimedOut){ //used this NSURLErrorTimedOut from foundation error responses 
      [self timedOut]; 
     }else if (error != nil){ 
      [self downloadError:error]; 
     } 
    }]; 
} 

現在設置一個被初始化的.h文件,並要求在if語句以上

- (void)receivedData:(NSData *)data 
{ 
    NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", newStr); //logs recived data 
    //now you can see what data is coming in on your log 
    //do what you want with your data here (i.e. start parsing methods 
} 
- (void)emptyReply 
{ 
    //not sure what to do here yet? 
} 
- (void)timedOut 
{ 
    //also not sure what to do here yet? 
} 
- (void)downloadError:(NSError *)error 
{ 
    NSLog(@"%@", error); 
    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"A connection failure occurred." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [errorAlert show]; 
} 

酷的方法,使相當多的基礎知識我在那裏做了什麼..現在我的問題如下。

問題一: 哪裏我打電話NSURLConnection的,像這樣

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
     { 

這裏發生的事情是什麼^的是,執行的是整個街區(包括if語句)在不同的線程或東西?因爲它看起來很像大中央調度格式但略有不同。

問題二: 我應該怎麼裏面emptyReply & TIMEDOUT方法來幹什麼?

問題三: 如何將緩存合併到此?我想緩存從不同請求中獲得的回覆。即與我的setRequestString你會看到有一個字符串輸入參數,所以我可以請求不同的rss飼料用相同的方法..我需要弄清楚如何緩存這些響應到單個緩存..但我不知道從哪裏開始它。

終於 如果您已經到目前爲止,非常感謝您閱讀我的問題。希望你的迴應能夠讓我們得到一個非常好的解決方案,在這裏..其他人可以自己使用,並挑選他們需要的位和peices,爲自己的解決方案工作..

無論如何非常感謝你閱讀,我期待着您的答覆..即使他們只是參考教程或例子,你認爲可能會幫助我..任何事情都很好我只想完全理解正在發生的事情以及最佳解決方案。

回答

5
  1. 閱讀蘋果文檔中的塊。它的新。或者你也可以閱讀here
  2. 您可以顯示錯誤,例如請求超時等你真的沒有分開處理這些不是一個錯誤,除非你有特殊的邏輯。
  3. 試試這個緩存

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL]; 
    
+0

感謝您的答覆。酷將閱讀關於這個arvo塊。至於緩存例子..我不想不緩存響應..我想緩存響應。但是我不知道如何處理它們..如果我需要在此另一種方法..還是我把我的NSURLConnection的成if語句,然後再次使用此https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html發現不同cahce政策謝謝迴應..我現在要去做一些研究塊.. – 2012-02-15 02:13:14

+0

很好的答案 – 2015-12-31 12:25:45