1
我正在設置一個包含數百個位置點的文檔的異步發佈請求。我想將這個文件保存在我的couchdb中。每個「POST」異步請求是否有任何文檔大小限制?
對於小文檔,請求響應在很短的時間內返回,存儲文檔。當文檔增長一點點(仍然是<〜200k)時,響應需要很長時間,然後隨着超時返回。我將超時設置爲120秒。
我是否缺少一些設置或編碼錯誤?
- (void)post:(NSData*) requestBody {
startTime = [NSDate date];
NSURL* serverURL = [NSURL URLWithString: @"https://myUser:[email protected]/myDB"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:120.0];
[request setHTTPBody:requestBody];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (response != nil) {
if ([response respondsToSelector:@selector(statusCode)])
{
int statusCode = [((NSHTTPURLResponse *)response) statusCode];
NSLog(@"StatusCode returned was %i", statusCode);
}
NSLog(@"Response: %@",response);
}
if ([data length] >0 && error == nil)
{
NSLog(@"data: %@", data);
// DO YOUR WORK HERE
}
else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}
}];
編輯:超時的問題仍然存在。我登錄了didSendBodyData中的進度:並確認發送了字節。不是全部,那麼發生超時。通過捲曲,上傳工作得很好。
此外,我承認一件事在日誌中:當請求成功,總的和預期的字節是相同的:
Total Bytes written: 161120
Expected Bytes to write: 161120
When the request failed:
Total Bytes written: 163840
Expected Bytes to write: 166839
任何其他的想法?
- (void)post:(NSData*) requestBody {
NSURL* serverURL = [NSURL URLWithString: @"https://mySpace.cloudant.com/myDb"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
#pragma mark -
#pragma mark Connection Delegates
- (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"myUser"
password:@"myPwd"
persistence:NSURLCredentialPersistenceForSession];
NSLog(@"credential created");
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
NSLog(@"responded to authentication challenge");
}
else {
NSLog(@"previous authentication failure");
}
}
檢查你的服務器配置,而不是iOS。如果有限制,則在服務器端設置。 –
有幾件事可能會導致這種情況。我懷疑你是否遇到任何請求大小限制(我認爲Cloudant中的最大文檔大小是64MB)。我的第一步是查看是否可以使用例如捲曲。如果您仍然超時,請發送電子郵件至[email protected],我們將盡力解決您的問題。 –