UITableViewController가 있습니다. TableViewControllerA라고합니다. API와 통신하기 위해 만든 다른 개체 APICallerB의 대리인입니다. NSURLSessionDataTask를 통해 APICallerB는 TableViewControllerA의 속성 중 하나와 동일하게 설정 될 속성 중 하나를 설정합니다. self.property1 = acb.property2
가 작동 할 수 있도록, 무엇을 완료하는 [acb makeAPICallWithARgument:self.argument]
기다릴 수있는 가장 좋은 방법 :GCD NSURLSessionDataTask가 완료 될 때까지 대기
- (void)viewDidLoad {
[super viewDidLoad];
// init instance of APICallerB
APICallerB *acb = [[APICallerB alloc] init];
// Set TableViewControllerA as delegate
tvcA.delegate = self;
[acb makeAPICallWithArgument:self.argument];
self.property1 = acb.property2;
}
내 질문은 :
여기 tableViewControllerA의 viewDidLoad
방법입니까? 나는 GCD가 사용될 수 있다고 가정하고있다. (`dispatch_sync '?) iOS/Objective-C에 익숙하지 않다. 아니면 다른 곳으로 이동하는 것이 좋을까요?
여기 APICallerB에서 방법 : 당신은 비동기 패턴을 사용한다, 그래서
- (void)makeAPICallWithArgument:(NSString *)arg
{
NSString *requestString = [NSString stringWithFormat:@"http://%@:%@@apiurl.com/json/Request?arg=%@", API_USERNAME, API_KEY, arg];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
config.HTTPAdditionalHeaders = @{@"Accept" : @"application/json"};
NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *ar = [jsonObject[@"Result"] objectForKey:@"results"];
self.property2 = ar;
}];
[dataTask resume];
}
disparch_semaphore는 사용할 수있는 옵션입니다. 이 블로그 게시물을 살펴보십시오. http://www.g8production.com/post/76942348764/wait-for-blocks-execution-using-a-dispatch-semaphore – HorseT
차가움. 나는 그것을 탐험 할 것이다. – Ja5onHoffman