簡體   English   中英

從主捆綁包復制創建的文件大小為零kb

[英]copying from main bundle creates a file size zero kb

作為我的應用程序啟動的一部分,我將捆綁文件復制到我的文檔目錄中。

我的四個文件中的三個都可以正常工作,但是第四個文件可以創建一個零KB文件。

在iOS 5.0 sim上運行。 我已經多次清理了構建,並檢查了文件名大小寫是否正確。

該文件出現在目錄中,但為零kb,應為24K

任何幫助表示贊賞。

-(BOOL) CheckDBs: (NSString *)dbname 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];   
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL success = [fileManager fileExistsAtPath: dbPath];
    NSLog(@"AppDelegate CheckDatabase: %@ = %i", dbPath, success);

    if (success) {
        //NSLog(@"return YES");
        return YES;
    }
    else {
        return NO;  
    }   
}  // Complete - checks if files exist in the User Documents directory

-(void) copyDBs: (NSString *) dbname 
{
    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];        
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname];
    BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

    if (success) {

        // Version 4.0 code
        //NSDictionary *attribs = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
        //success = [fileManager setAttributes:attribs ofItemAtPath:dbPath error:&error];
        NSLog(@"AppDelegate copyDatase: %@ = %d", dbPath, success); 
    }

    //NSLog(@"AppDelegate copyDatase: %@ = %d", dbPath, success);   
    if (!success) {

        NSLog(@"Failed to copy database: '%@'", [error localizedDescription]);
        //  NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }   
}

您還檢查了原始文件大小嗎?

嘗試重置您的模擬器。 NSFileManager文檔中:

如果dstPath中已經存在同名文件,則此方法將中止復制嘗試,並返回適當的錯誤。

確保目的地為空,然后重試。 另外,檢查error對象。

如果全部檢查出,文件名拼寫肯定出錯。 檢查確切的文件是否存在於捆綁軟件,NSLog中,無論您在何處使用文件名或路徑等。您都應該找到錯誤。 還要在Finder中檢查相應的文件夾。

而不是使用

[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname]

嘗試

[[NSBundle mainBundle] pathForResource:shortName ofType:@"db"]

好的,我弄清楚是什么原因引起的。

當我運行應用程序時,在加載視圖控制器之一之前,appdidfinishlaunching方法未完成。 該視圖控制器嘗試訪問從分發包復制過來的文件之一。

我猜sqlite在嘗試訪問數據庫時會創建文件,它會以零字節長度創建文件。

因此,當我的appdidfinish啟動方法檢查文件是否存在時,由於sql調用而導致文件存在。

這通常只會在應用程序首次運行之前出現問題,因為之后數據庫將存在。

現在的問題是,在有問題的視圖控制器是mainwindow.xib的一部分時,如何使appdidfinish啟動完成,然后其余的啟動?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM