먼저 질문의 답은 실패입니다. 만약이 게시물의 맨 아래로 건너 뛰면 굵게 표시됩니다. 자세한 내용은 나머지를 읽을 수 있습니다. 이 게시물의 ...nsurlconnection 비동기 요청
나는 NSURLConnection을 원활하게 작동시켜 원활하게 작동하고 올바르게 이해할 수 있도록 노력하고 있습니다. 인터넷에 비동기 연결을위한 예제/튜토리얼의 심오한 부족이 있습니다. 연결을 설정하고 실행하는 것 이외의 다른 수준에서 어떤 일이 일어나는지 설명 할 수는 없습니다. 다행히도이 질문은 다른 사용자를위한 것입니다.
내 .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;
그래서 내 헤더 파일을 먹으 렴 .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];
}
}];
}
지금
- (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 파일에 초기화라고했다 방법을 설정하는 것이 거의 기본 너무 내가 지금 한 일은 .. 지금 내가 가진 질문은 다음과 같다.질문 하나 : 내가 그렇게
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
같이있는 NSURLConnection를 호출 은 여기에서 일어나고있는 것은 무엇인가 ^위한입니다 다른 스레드 또는 무언가에 (문 경우 포함)이 전체 블록을 실행 ? 중앙 집중식 파견 형식과 비슷하지만 약간 다릅니다.
질문이 : 내가 emptyReply & TIMEDOUT 방법 안에 무엇을 일을해야 하는가?
질문 3 : 어떻게 캐싱을이 기능에 통합합니까? 나는 다른 요청에서 얻은 응답을 캐시하고 싶습니다. 즉 내 setRequestString 함께 문자열 입력 매개 변수가 표시됩니다, 그래서 같은 방법으로 다른 RSS 피드를 요청할 수 있습니다 .. 어떻게 개별 캐시에 이러한 응답을 캐시 할 알아낼 필요가 있지만 어디서부터 시작 해야할지 모르겠다. 그것.
마지막으로 여기까지 작성하신 경우, 제 질문을 읽어 주셔서 감사합니다. 바라건대 여러분의 응답으로 우리는 꽤 좋은 솔루션을 얻을 수 있습니다. 다른 사람들이 스스로 사용할 수 있고 자신이 원하는 해결책을 선택할 수 있습니다.
어쨌든 고맙습니다 독서와 나는 너의 대답을 고대한다. ..비록 그들이 자습서 나 예제에 대한 조언 만한다면 당신은 나를 도울 수있을 것이라고 생각합니다. 무엇이 좋을까요? 나는 단지 무슨 일이 벌어지고 있고 좋은 해결책이 무엇인지 완전히 이해하기를 원합니다.
응답에 대한 감사를 위해 이것을보십시오. Cool은이 arvo 블록을 읽습니다. 귀하의 캐시 예제에 관해서 .. 내가 캐시 응답을하고 싶지 않아 .. 내가 응답을 캐시하고 싶습니다 .. 그러나 나는 그들을 처리하는 방법을 잘 모르겠습니다 .. 만약 내가 다른 방법이 필요합니다 .. 아니면 내가 할 내 nsurlconnection을 if 문에 넣고 다른 cahce 정책을 사용하십시오 https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html 다시 감사드립니다. 응답 .. 지금가는 블록에 대한 연구를 할거야 .. –
아주 좋은 대답 –