【发布时间】:2017-04-13 13:04:11
【问题描述】:
我已将 NSURLConnection 代码替换为 NSURLSession 以使用 EWS Exchange 服务器获取数据。我的应用程序进行了多个并发 API 调用。它工作正常,但现在,当我使用 NSURLSession 时,我的一些 API 调用得到正确的响应,而一些从交换服务器得到错误如下:
{
"s:Envelope" = {
"s:Body" = {
"m:GetItemResponse" = {
"m:ResponseMessages" = {
"m:GetItemResponseMessage" = {
ResponseClass = Error;
"m:DescriptiveLinkKey" = {
text = 0;
};
"m:Items" = {
};
"m:MessageText" = {
text = "An internal server error occurred. The operation failed., Cannot open mailbox /o=First Organization/ou=Exchange Administrative Group(FYDIBOHF23SPDLT)/cn=Recipients/cn=00037FFEE6F0D3D2.";
};
"m:MessageXml" = {
"t:Value" = (
{
Name = InnerErrorMessageText;
text = "Too many concurrent connections opened.";
},
{
Name = InnerErrorResponseCode;
text = ErrorTooManyObjectsOpened;
},
{
Name = InnerErrorDescriptiveLinkKey;
text = 0;
}
);
};
"m:ResponseCode" = {
text = ErrorInternalServerError;
};
};
};
"xmlns:m" = "http://schemas.microsoft.com/exchange/services/2006/messages";
"xmlns:t" = "http://schemas.microsoft.com/exchange/services/2006/types";
};
"xmlns:xsd" = "http://www.w3.org/2001/XMLSchema";
"xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance";
};
"s:Header" = {
"h:ServerVersionInfo" = {
MajorBuildNumber = 1034;
MajorVersion = 15;
MinorBuildNumber = 11;
MinorVersion = 1;
xmlns = "http://schemas.microsoft.com/exchange/services/2006/types";
"xmlns:h" = "http://schemas.microsoft.com/exchange/services/2006/types";
"xmlns:xsd" = "http://www.w3.org/2001/XMLSchema";
"xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance";
};
};
"xmlns:s" = "http://schemas.xmlsoap.org/soap/envelope/";
};
显然问题是同时连接太多。
我的代码流程是:
我的HTTPRequest.m有一个方法
- (void)fetchData
{
dispatch_async(dispatch_get_main_queue(), ^{
sessionAlive = sessionAlive + 1;
NSLog(@"sessionCount: %ld", (long)sessionAlive);
NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfiguration
delegate:self
delegateQueue:nil];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request];
[dataTask resume];
});
}
// Some NSURLSession delegates methods here
-(void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data
{
[self.data appendData:data];
}
-(void)URLSession:(NSURLSession *)session
task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
if (error)
{
self.failureBlock ? self.failureBlock(error) : nil;
}
else
{
NSData *data;
if (self.data)
{
data = [NSData dataWithData:self.data];
}
self.successBlock ? self.successBlock(self.redirectLocation, data) : nil;
}
[session finishTasksAndInvalidate]; // We must release the session, else it holds strong referance for it's delegate (in our case EWSHTTPRequest).
// And it wont allow the delegate object to free -> cause memory leak
}
我正在同时下载类似这样的电子邮件:
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(backgroundQueue, ^{
foreach (NSString *emailId in emalilIDArray)
{
HTTPRequest *request = [HTTPRequest alloc] init];
[request fetchData];
}
});
我认为我的问题是我在每次通话时都在进行会话,但我不知道看到其他方式。
如果我不能使用共享实例会话,因为我需要将会话的委托与每个 HTTPRequest 对象关联以处理响应。
有什么建议或更好的方法吗?
【问题讨论】:
标签: ios concurrency exchangewebservices nsurlsession