2013-05-15 134 views
1

我正在使用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鍵時,連接失敗不會發生。它只有在磁蓋蓋關閉或超時時纔會失效。

回答

2

當應用程序移到後臺時,操作系統會讓應用程序5s完成暫停前的操作(保持RAM,但停止應用程序接收任何消息或執行任何操作)。如果您有一項任務需要在用戶按下主頁按鈕時運行完成,則可以使用後臺任務。從蘋果公司的文檔:

應用程序委託的applicationDidEnterBackground:方法有 約5秒完成任何任務並返回。在實踐中, 這個方法應該儘快返回。如果方法確實 在時間耗盡之前沒有返回,則會終止您的應用並從內存中清除 內存。如果您仍然需要更多時間來執行任務,請致電 beginBackgroundTaskWithExpirationHandler:方法請求後臺 執行時間,然後在第 線程中啓動任何長時間運行的任務。無論您是否啓動任何後臺任務, applicationDidEnterBackground:方法仍必須在5秒鐘內退出。

注意:UIApplicationDidEnterBackgroundNotification通知是 也發送到讓您的應用程序的感興趣的部分知道它是輸入 的背景。您的應用中的對象可以使用默認通知 中心註冊此通知。

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

1

使用可達代碼試試這個代碼,以節省下載一次數據:

-(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]; 
} 
相關問題