让我清楚我的问题。 我想要一个通用的共享类来调用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.
更多推荐
发布评论