从主包复制创建的文件大小为零 kb

发布于 2024-12-17 04:52:53 字数 2154 浏览 1 评论 0原文

作为我的应用程序启动的一部分,我将捆绑文件复制到我的文档目录中。

这对于我的四个文件中的三个来说效果很好,但第四个文件创建了一个零 KB 文件。

在 iOS 5.0 sim 上运行。我已经清理了几次构建并检查了文件名大小写是否正确。

该文件出现在目录中,但大小为零,

如果有任何帮助,应该是 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]);
    }   
}

As part of my app start-up i copy bundle files to my documents directory.

This works fine for three out of four of my files but the fourth one create a Zero KB file.

running on iOS 5.0 sim. I have cleaned the build several times and checked the file name capitalization vis correct.

the file appears in the directory but is zero kb and should be 24K

any help appreciated.

-(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]);
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-12-24 04:52:53

您是否还检查过原始文件大小?

尝试重置您的模拟器。来自 NSFileManager 文档:

如果dstPath已经存在同名文件,则此方法
中止复制尝试并返回适当的错误。

确保目的地为空,然后重试。另外,检查 error 对象。

如果所有检查结果都正确,则文件名拼写一定有错误。检查捆绑包、NSLog 中是否存在确切的文件(无论您使用文件名或路径等)。您应该会发现错误。另请检查 Finder 中的相应文件夹。

而不是使用

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

尝试

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

Have you also checked the original file size?

Try resetting your simulator. From the NSFileManager documentation:

If a file with the same name already exists at dstPath, this method
aborts the copy attempt and returns an appropriate error.

Make sure the destination is empty and try again. Also, check the error object.

If all that checks out there has got to be an error in spelling the file name. Check if the exact file exists in bundle, NSLog wherever you use a file name or path, etc. You should find the error. Also check the appropriate folder in the Finder.

Instead of using

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

try

[[NSBundle mainBundle] pathForResource:shortName ofType:@"db"]
失去的东西太少 2024-12-24 04:52:53

好吧,我弄清楚是什么导致了这个问题。

当我运行应用程序时,appdidfinishlaunching 方法在加载视图控制器之一之前尚未完成。该视图控制器尝试访问从捆绑包复制过来的文件之一。

我猜测当您尝试访问数据库时 sqlite 会创建该文件,它会以零字节长度创建该文件。

因此,当我的 appdidfinish 启动方法检查文件是否存在时,该文件由于 sql 调用而存在。

这通常只会在首次运行应用程序之前成为问题,因为之后数据库将存在。

现在的问题是我如何让 appdidfinish 启动在其余部分允许启动之前完成,因为有问题的视图控制器是 mainwindow.xib 的一部分

Ok I figured out what is causing the problem.

as i run the app the appdidfinishlaunching method is not complete before one of the view controllers is loading. That view controller attempts to access one of the files being copied over from the bundle.

I'm guessing that sqlite creates the file when you attempt to access the database, it creates it with with a zero bytes length.

So when my appdidfinish launching method checks for the existance of the file it exists due to the sql call.

This is usually only going to be a problem prior to the first run of the app as after that the database will exist.

problem now is how do i get the appdidfinish launching to complete prior to the rest being allow to start as the view controller in question is part of the mainwindow.xib

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文