まず第一に、質問は失敗とみなされます。もしあなたがこの記事の一番下までスキップしているのを見たいのであれば、太字で表示されます。このポストの...nsurlconnection非同期リクエスト
私はNSURLConnectionを円滑にしようとしているので、円滑に動作し、正しく理解しています。インターネット上の非同期接続のサンプル/チュートリアルの深刻な欠如があります。接続を確立して実行している以外に、深刻なレベルで何が起こっているのかわかりません。うまくいけば、この質問は私が他のユーザーのためにそこにいると感じる空白をいっぱいにすることができます。
私の.hファイルでは、私は基礎ヘッダをインポートし、受信したデータの欠落(エラーなど)に必要なメソッドを宣言しました。
非常に単純多くの必要な説明ではないので、私のヘッダファイルのthatsの.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;
... 。今のかなりの基礎となるよう
- (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];
}
上記のif文でクールな.hファイルで初期化され、呼び出されたメソッドを設定
.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];
}
}];
}
私が今行っていること..今私が持っている質問は以下の通りです。
質問1: 私はそう
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
^が別のスレッドか何かに(if文を含む)は、その全体のブロックを実行していることを何のためにあるのか、ここで何が起こっているようNSURLConnectionを呼び出します?これは大規模な中央ディスパッチフォーマットのように見えますが少し異なります。
質問2:私はemptyReply & TIMEDOUTメソッドの内部で何を をやるべき?
質問3: どのようにしてキャッシュに組み込むことができますか?私はさまざまな要求から返ってくる応答をキャッシュしたいと思います。私のsetRequestStringを使用すると、文字列入力パラメータがあることがわかるので、同じメソッドで別のrssフィードをリクエストすることができます。これらのレスポンスを個々のキャッシュにキャッシュする方法を理解する必要がありますが、それ。
最後に これまでに作成したことがありましたら、私の質問を読んでいただきありがとうございます。うまくいけば、あなたはここに行くかなりいい解決策を得ることができます。他の人たちが自分のために使うことができ、彼らがそこに自分の解決策のために働く必要があるビットとピースを選択して選ぶことができます..
どうもありがとう読んで、私はあなたの返信を楽しみにしています..たとえチュートリアルやサンプルを参考にして助けてくれると思っても、何かいいです。何が起こっているのか十分に理解したいと思っています。
応答への感謝のためにこれを試してみてください。クールはこのarvoのブロックを読み上げます。あなたのキャッシュの例に関しては..私は応答をキャッシュしたくない。私は応答をキャッシュしたい..しかし、私はそれらを処理する方法がわかりません..私はこのために別の方法が必要な場合..または私は私のnsurlconnectionをifステートメントに入れて、ここにあるさまざまなcahceポリシーを使用してくださいhttps://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.htmlもう一度ありがとう応答..今ブロックのいくつかの研究を行うつもりです.. –
非常に良い答え –