我正在使用CMIS(Content management interoperability services)
从露天服务器下载数据。我正在使用以下代码,它在某种程度上可以正常工作,但是当应用程序进入后台时,网络连接丢失,当应用程序进入前台时,它会尝试重试下载并失败说连接错误。由于我是新手,任何帮助将不胜感激。
- (void)testFileDownload
{
[self runTest:^
{
[self.session retrieveObjectByPath:@"/ios-test" completionBlock:^(CMISObject *object, NSError *error) {
CMISFolder *testFolder = (CMISFolder *)object;
STAssertNil(error, @"Error while retrieving folder: %@", [error description]);
STAssertNotNil(testFolder, @"folder object should not be nil");
CMISOperationContext *operationContext = [CMISOperationContext defaultOperationContext];
operationContext.maxItemsPerPage = 100;
[testFolder retrieveChildrenWithOperationContext:operationContext completionBlock:^(CMISPagedResult *childrenResult, NSError *error) {
STAssertNil(error, @"Got error while retrieving children: %@", [error description]);
STAssertNotNil(childrenResult, @"childrenCollection should not be nil");
NSArray *children = childrenResult.resultArray;
STAssertNotNil(children, @"children should not be nil");
STAssertTrue([children count] >= 3, @"There should be at least 3 children");
CMISDocument *randomDoc = nil;
for (CMISObject *object in children)
{
if ([object class] == [CMISDocument class])
{
randomDoc = (CMISDocument *)object;
}
}
STAssertNotNil(randomDoc, @"Can only continue test if test folder contains at least one document");
NSLog(@"Fetching content stream for document %@", randomDoc.name);
// Writing content of CMIS document to local file
NSString *filePath = [NSString stringWithFormat:@"%@/testfile", NSTemporaryDirectory()];
// NSString *filePath = @"testfile";
[randomDoc downloadContentToFile:filePath
completionBlock:^(NSError *error) {
if (error == nil) {
// Assert File exists and check file length
STAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:filePath], @"File does not exist");
NSError *fileError = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&fileError];
STAssertNil(fileError, @"Could not verify attributes of file %@: %@", filePath, [fileError description]);
STAssertTrue([fileAttributes fileSize] > 10, @"Expected a file of at least 10 bytes, but found one of %d bytes", [fileAttributes fileSize]);
// Nice boys clean up after themselves
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&fileError];
STAssertNil(fileError, @"Could not remove file %@: %@", filePath, [fileError description]);
} else {
STAssertNil(error, @"Error while writing content: %@", [error description]);
}
self.testCompleted = YES;
} progressBlock:nil];
}];
}];
}];
}
当用户按下 home 键时不会发生连接失败。仅当磁盖关闭或超时时才会失败。