用于webservice调用的公共共享类(Common shared class for webservice call)

编程入门 行业动态 更新时间:2024-10-18 08:21:35
用于webservice调用的公共共享类(Common shared class for webservice call)

让我清楚我的问题。 我想要一个通用的共享类来调用web服务。 而且我还想使用Block来处理响应中的数据和错误。

我得到了答案。 正如阿明所说,我可以如下所示:

+ (instancetype)sharedInstance { static NetworkManager *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[NetworkManager alloc]init]; NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:sharedInstance delegateQueue:nil]; }); return sharedInstance; }

之后我可以定义如下方法:

-(void) fetchDataForURL : (NSString*)urlString postData:(NSDictionary*)dataDic WithCompletionBlock : (void(^) (NSDictionary *responseDictionary, NSError *error)) completionBlock { // sending request here using NSURlConnection or NSURLSession // whatever data I get here I can get that data in the completionBlock declared in the method completionBlock(jsonDic, error); }

现在,我可以使用共享实例在任何类中调用该方法。 而且我也可以在完成块中获取数据。

希望我说清楚一点。 感谢你的帮助。

Let me clear my question. I want a common shared class to call a webservice. And also I want to handle the data and error I get in the response using a Block.

I got the answer for that. As Amin said I can make a shared insctance as below:

+ (instancetype)sharedInstance { static NetworkManager *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[NetworkManager alloc]init]; NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:sharedInstance delegateQueue:nil]; }); return sharedInstance; }

After that I can define a method as below:

-(void) fetchDataForURL : (NSString*)urlString postData:(NSDictionary*)dataDic WithCompletionBlock : (void(^) (NSDictionary *responseDictionary, NSError *error)) completionBlock { // sending request here using NSURlConnection or NSURLSession // whatever data I get here I can get that data in the completionBlock declared in the method completionBlock(jsonDic, error); }

Now, I can call the method in any class using the shared instance. And also I can get the data in the completion block.

Hope I made it little clear. Thanks all for your help.

最满意答案

我很不确定,你也想做什么。 基本上我理解你的Q,你希望在一个NetworkManager类的一个共享实例中获得所有请求的响应? 对?

首先,你应该重新考虑你的设计。 “收集所有的一个”就像一个全局的var代码气味。 但是,......

A.创建网络管理器的共享实例。 这是通常的模式:

@implemenation NetworkManager … + (instancetype)sharedInstance { static NetworkManager *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[NetworkManager alloc] init]; }); return sharedInstance; }

B.在完成块中使用共享实例。 例如:

[NSURLSession dataTaskWithURL:… completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { NetworkManager *networkManager = [NetworkManager sharedInstance]; [networkManager processData:data]; // Or whatever you want to do. }

此外,将网络管理器设置为会话的委托可能很有用。

I'm pretty unsure, what you want to do, too. Basically I understand your Q that way that you want to get the responses of all requests in one shared instance of a class NetworkManager? Right?

First of all you should re-think your design. "Collecting all in one" is like a global var code smell. However, …

A. Create a shared instance of the network manager. This is the usual pattern:

@implemenation NetworkManager … + (instancetype)sharedInstance { static NetworkManager *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[NetworkManager alloc] init]; }); return sharedInstance; }

B. Use the shared instance in a completion block. For example:

[NSURLSession dataTaskWithURL:… completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { NetworkManager *networkManager = [NetworkManager sharedInstance]; [networkManager processData:data]; // Or whatever you want to do. }

Additionally it might be useful to set the network manager as delegate of the session.

更多推荐

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

发布评论

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

>www.elefans.com

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