0

我正在尝试使用 Core Data 在我的应用程序上制作一个小字典。当您使用 Master-Detail 应用程序时,会在用户的 Documents 文件夹中创建 xxx.sqlite 文件。现在在我第一次启动应用程序之前,我将代码更改如下,因为我想要应用程序中的 xxx.sqlite 文件:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
}

    //NSURL *storeURL = [[self applicationDocumentsDirectory]
                               URLByAppendingPathComponent:@"CoreDataFileTest.sqlite"];

    NSString* path= [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CoreDataTest.sqlite"];

NSURL* storeURL = [[NSURL alloc] initFileURLWithPath:path];

我认为最后两行会在我的应用程序项目文件夹中创建一个 xxx.sqlite 文件。事实上,它没有。但该应用程序运行良好。这意味着 xxx.sqlite 文件是嵌入在应用程序本身中的吗?感谢您的时间。

4

2 回答 2

1

如果,据我了解您的问题,您想在您的应用程序中包含一个默认数据库(即 .sqlite 文件将作为您项目的资产包含在内,那么您需要将它从应用程序包复制到您的文档目录中启动)。

然后从您的应用委托中的 didFinishLaunchingWithOptions 方法调用此函数。

- (void) copyDefaultDB
{
    // If we are running the app for the first time, then copy our default database across from our app directory
    // into our user directory 
    NSString *filePath;

    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex: 0];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    filePath = [documentsDir stringByAppendingPathComponent: @"CoreDataFileTest.sqlite"];

    // If the database already exists then return without doing anything
    if (![fileManager fileExistsAtPath: filePath])
    {
        // Now copy the new shiny one in

        NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"CoreDataFileTest.sqlite"];

        // Copy the database from the package to the users filesystem
        [fileManager copyItemAtPath: filePathFromApp 
                             toPath: filePath 
                              error: nil];
    }
}
于 2013-07-27T13:52:05.150 回答
0

试试这个代码。在AppDelegate.m中写入

- (NSManagedObjectContext *) managedObjectContext {
    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }

    return managedObjectContext;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
                                               stringByAppendingPathComponent: @"ProjectManagement.sqlite"]];
    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                                   initWithManagedObjectModel:[self managedObjectModel]];
    if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                 configuration:nil
                                                           URL:storeUrl
                                                       options:nil error:&error]){
        /*Error for store creation should be handled in here*/
    }

    return persistentStoreCoordinator;
}

- (NSManagedObjectModel *)managedObjectModel {
    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];

    return managedObjectModel;
}

- (NSString*)applicationDocumentsDirectory{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
}
于 2013-07-29T03:56:26.277 回答