NSPersistentStoreCoordinator数据库存储位置applicationSupportFolder问题

时间:2013-10-13 16:23:06

标签: cocoa

现在我的应用程序将数据库放在〜/ Library文件夹中但是我希望它以更有条理的方式放置〜/ Library / App Name但是我无法弄清楚如何使用这个块来实现代码。

(NSPersistentStoreCoordinator *) persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}

NSFileManager *fileManager;
NSString *applicationSupportFolder = nil;
NSString *dataFilePath;
NSURL *url;
NSError *error;

fileManager = [NSFileManager defaultManager];
applicationSupportFolder = [self applicationSupportFolder];
if ( ![fileManager fileExistsAtPath:applicationSupportFolder isDirectory:NULL] ) {
    [fileManager createDirectoryAtPath:applicationSupportFolder attributes:nil];
}


dataFilePath = [applicationSupportFolder stringByAppendingPathComponent: @"sample.dat"];


if( NO == [[ NSFileManager defaultManager ] fileExistsAtPath: dataFilePath ] )
{

    [[ NSFileManager defaultManager ] copyPath: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"sample.dat"]
        toPath: dataFilePath handler: NULL ];

}

url = [NSURL fileURLWithPath:dataFilePath];
if( url )
{           
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];


    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]){

        [[NSApplication sharedApplication] presentError:error];
    }    
}
return persistentStoreCoordinator;
}

这段代码可以将数据库放在一个非常糟糕的位置IMO中。我是Obj-C的新手,只是继承了另一个开发人员的代码集。

更新:

添加下面的最终工作代码。

(NSPersistentStoreCoordinator *) persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}

NSFileManager *fileManager;
NSString *applicationSupportFolder = nil;
NSString *dataFilePath;
NSURL *url;
NSError *error;

fileManager = [NSFileManager defaultManager];
applicationSupportFolder = [self applicationSupportFolder];
NSString *aappSupportFolder = [applicationSupportFolder stringByAppendingPathComponent: @"APP"];

if ( ![fileManager fileExistsAtPath:aappSupportFolder isDirectory:NULL] ) {
    [fileManager createDirectoryAtPath:aappSupportFolder attributes:nil];
}

dataFilePath = [aappSupportFolder stringByAppendingPathComponent: @"sample.dat"];


if( NO == [[ NSFileManager defaultManager ] fileExistsAtPath: dataFilePath ] )
{

    [[ NSFileManager defaultManager ] copyPath: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"sample.dat"]
        toPath: dataFilePath handler: NULL ];

}

url = [NSURL fileURLWithPath:dataFilePath];
if( url )
{           
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];


    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]){

        [[NSApplication sharedApplication] presentError:error];
    }    
}
return persistentStoreCoordinator;
}

1 个答案:

答案 0 :(得分:1)

很简单,只需要在机器的库文件夹中创建文件夹名称APP,然后就必须在同一路径中移动应用程序。请尝试以下: -

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *pathString= [paths objectAtIndex:0];
 NSError *err=nil;
//below is your application previous path which we will move to new path
NSString  *dataFilePath = [pathString stringByAppendingPathComponent: @"sample.dat"];
NSString *applicationSupportFolder = [pathString stringByAppendingPathComponent: @"App"];
//Here we have created directory folder as App inside Library below
if ( ![fileManager fileExistsAtPath:applicationSupportFolder isDirectory:NULL] ) {
    [fileManager createDirectoryAtPath:applicationSupportFolder attributes:nil];
}

   if ([fm moveItemAtPath:dataFilePath toPath:applicationSupportFolder error:&err])
        {
            NSLog(@"success");
        }
 else
        {
            NSLog(@"%@",[err localizedDescription]);
        }