当应用程序进入后台时,网络连接正在丢失

时间:2013-05-15 18:23:11

标签: iphone objective-c cmis

我正在使用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];
             }];
         }];
     }];
}

当用户按下主页键时,不会发生连接失败。只有当磁性盖子关闭或超时时它才会失效。

2 个答案:

答案 0 :(得分:2)

当应用程序移动到后台时,操作系统会让应用程序5s在挂起之前完成它正在做的事情(保留RAM,但停止应用程序接收任何消息或做任何事情)。如果您有一个任务需要在用户按下主页按钮时运行完成,则可以使用后台任务。来自apple的文档:

  

你的app delegate的applicationDidEnterBackground:方法有   大约5秒钟完成任务并返回。在实践中,   这种方法应该尽快返回。如果方法有   在时间用完之前没有返回,你的应用程序被杀死并被清除   记忆。如果您还需要更多时间来执行任务,请致电   beginBackgroundTaskWithExpirationHandler:请求背景的方法   执行时间然后启动辅助中的任何长时间运行的任务   线。无论你是否开始任何后台任务,都可以   applicationDidEnterBackground:方法必须仍然在5内退出   秒。

     

注意:UIApplicationDidEnterBackgroundNotification通知是   也发送给你的应用程序的感兴趣的部分知道它正在进入   的背景。您应用中的对象可以使用默认通知   中心注册此通知。

来自http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

答案 1 :(得分:1)

USE可访问性代码尝试使用此代码在下载后保存数据:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
//try to access that local file for writing to it...
NSFileHandle *hFile = [NSFileHandle fileHandleForWritingAtPath:self.localPath];
//did we succeed in opening the existing file?
if (!hFile) 
{   //nope->create that file!
   [[NSFileManager defaultManager] createFileAtPath:self.localPath contents:nil attributes:nil];
   //try to open it again...
   hFile = [NSFileHandle fileHandleForWritingAtPath:self.localPath];
}
//did we finally get an accessable file?
if (!hFile)
{   //nope->bomb out!
   NSLog("could not write to file %@", self.localPath); 
   return;
}
//we never know - hence we better catch possible exceptions!
@try 
{
   //seek to the end of the file
   [hFile seekToEndOfFile];
   //finally write our data to it
   [hFile writeData:data];
}
@catch (NSException * e) 
{
   NSLog("exception when writing to file %@", self.localPath); 
   result = NO;
}
[hFile closeFile];
}