まず第一に、質問はfailry simipleです..それらが何であるかを確認したい場合は、この投稿の最後までスキップしてください。太字で表示されます..詳細については、この投稿の残りを読むことができます...
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];
}
クールなので、私がそこでやったことのほとんどの基本..今、私が持っている質問は次のとおりです。
質問 1: NSURLConnection を呼び出す場所
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
ここで何が起こっているのか ^ for は、そのブロック全体 (if ステートメントを含む) を別のスレッドまたは何かで実行することですか? グランドセントラルディスパッチのフォーマットによく似ていますが、わずかに異なるためです。
質問 2: emptyReplyおよびtimedOutメソッド 内で何をすればよいですか?
質問 3: これにキャッシュを組み込むにはどうすればよいですか? さまざまなリクエストから返されたレスポンスをキャッシュしたいと思います。つまり、私の setRequestString を使用すると、文字列入力パラメーターがあることがわかります。そのため、同じメソッドで異なる RSS フィードを要求できます..これらの応答を個々のキャッシュにキャッシュする方法を理解する必要があります..しかし、どこから始めればよいかわかりませんそれ。
最後 に ここまで読んでくれてありがとう。うまくいけば、あなたの回答で、私たちはここでかなり素晴らしい解決策を得ることができます..他の人が自分で使用し、必要なビットとピースを選んで選択して、独自のソリューションに機能させることができます..
とにかく、読んでくれてありがとう。あなたの返信を楽しみにしています..たとえそれらがチュートリアルやあなたが私を助けると思う例への言及であっても..何でも良いです何が起こっているのか、何が良い解決策なのかを完全に理解したいだけです. .