我有一个方法向服务器发送请求,我已使用completionHandler创建了对该函数的调用,如下所示:
-(void)sendAddSubscriptionRequest:(NSString*)owner withComppletionHandler:(void (^)(BOOL, NSArray *, NSError *))completion dataWebService = [[NSMutableData alloc] init]; NSURL* aUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.google.com/add?"]]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:aUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; [request addValue:[NSString stringWithFormat:@"Bearer %@", "token"] forHTTPHeaderField:@"Authorization"]; [request setHTTPMethod:@"POST"]; NSString* postData = [[NSString alloc] initWithFormat:@"owner=%@", owner]; [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]]; NSData* returnData = [NSURLConnection sendAsynchronousRequest:request queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { };我的问题是如何在获取数据后将完成处理程序返回到我的函数。
谢谢,。
I have a method which send request to server and i have created the call to that function with completionHandler as follows:
-(void)sendAddSubscriptionRequest:(NSString*)owner withComppletionHandler:(void (^)(BOOL, NSArray *, NSError *))completion dataWebService = [[NSMutableData alloc] init]; NSURL* aUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.google.com/add?"]]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:aUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; [request addValue:[NSString stringWithFormat:@"Bearer %@", "token"] forHTTPHeaderField:@"Authorization"]; [request setHTTPMethod:@"POST"]; NSString* postData = [[NSString alloc] initWithFormat:@"owner=%@", owner]; [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]]; NSData* returnData = [NSURLConnection sendAsynchronousRequest:request queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { };My problem is how should i return completion handler to my function after the data is fetched.
Thanks,.
最满意答案
当您发送异步请求时,该方法将立即返回,因为数据将在NSURLConnection完成处理程序中“返回”。 然后,您可以在那里对其进行解码并将其传递给您自己的完成处理程序:
[NSURLConnection sendAsynchronousRequest:request queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSArray *array = nil; if (!connectionError) array = /* get from data somehow */ completion(!connectionError, array, connectionError); }];注意:目前还不清楚你的完成处理程序参数是什么意思,所以我可能已经采取了一些自由。
As you are sending an asynchronous request, the method will return immediately as the data will be "returned" in the NSURLConnection completion handler. You can then decode it there and pass it along to your own completion handler:
[NSURLConnection sendAsynchronousRequest:request queue:nil completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSArray *array = nil; if (!connectionError) array = /* get from data somehow */ completion(!connectionError, array, connectionError); }];Note: It's not clear what your completion handler parameters mean, so I might have taken some liberties.
更多推荐
发布评论