从服务器请求信息的方法中的完成处理程序(completion handler in method which request information from server)

编程入门 行业动态 更新时间:2024-10-27 06:32:49
服务器请求信息的方法中的完成处理程序(completion handler in method which request information from server)

我有一个方法向服务器发送请求,我已使用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.

更多推荐

本文发布于:2023-07-24 00:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   程序   方法   信息   completion

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!