网络服务调用的公共共享 class
Common shared class for webservice call
让我澄清一下我的问题。我想要一个共同的共享 class 来调用网络服务。而且我还想处理我在使用 Block 的响应中获得的数据和错误。
我得到了答案。正如 Amin 所说,我可以创建如下共享实例:
+ (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);
}
现在,我可以使用共享实例调用任何 class 中的方法。而且我还可以在完成块中获取数据。
希望我说得不够清楚。
感谢大家的帮助。
我很不确定,你也想做什么。基本上我理解你的问题是你想在 class NetworkManager
的一个共享实例中获得所有请求的响应?对吗?
首先你应该重新考虑你的设计。 "Collecting all in one" 就像是全球变种代码的味道。然而,...
一个。创建网络管理器的共享实例。这是通常的模式:
@implemenation NetworkManager
…
+ (instancetype)sharedInstance
{
static NetworkManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken,
^{
sharedInstance = [[NetworkManager alloc] init];
});
return sharedInstance;
}
乙。在完成块中使用共享实例。例如:
[NSURLSession dataTaskWithURL:… completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error)
{
NetworkManager *networkManager = [NetworkManager sharedInstance];
[networkManager processData:data]; // Or whatever you want to do.
}
此外,将网络管理员设置为会话的代表可能会有用。
让我澄清一下我的问题。我想要一个共同的共享 class 来调用网络服务。而且我还想处理我在使用 Block 的响应中获得的数据和错误。
我得到了答案。正如 Amin 所说,我可以创建如下共享实例:
+ (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);
}
现在,我可以使用共享实例调用任何 class 中的方法。而且我还可以在完成块中获取数据。
希望我说得不够清楚。 感谢大家的帮助。
我很不确定,你也想做什么。基本上我理解你的问题是你想在 class NetworkManager
的一个共享实例中获得所有请求的响应?对吗?
首先你应该重新考虑你的设计。 "Collecting all in one" 就像是全球变种代码的味道。然而,...
一个。创建网络管理器的共享实例。这是通常的模式:
@implemenation NetworkManager
…
+ (instancetype)sharedInstance
{
static NetworkManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken,
^{
sharedInstance = [[NetworkManager alloc] init];
});
return sharedInstance;
}
乙。在完成块中使用共享实例。例如:
[NSURLSession dataTaskWithURL:… completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error)
{
NetworkManager *networkManager = [NetworkManager sharedInstance];
[networkManager processData:data]; // Or whatever you want to do.
}
此外,将网络管理员设置为会话的代表可能会有用。