核心数据问题

时间:2009-09-08 05:02:17

标签: iphone sqlite core-data ios-simulator desktop-application

我正在尝试编写两个应用程序(iphone和桌面)来实现以下链接中描述的内容:

core-data-is-it-possible-to-build-a-desktop-app-to-create-the-data-model-for-an

确定。所以我创建了一个非常简单的桌面应用程序,它有一个名为Client的实体,带有一个名为name的字符串属性字段。我还生成了相应的Model类。

我运行应用程序在列表中添加了几个客户端名称并保存了文件(作为Testing.sqlite)。

现在在我的等效iphone应用程序中,我正在尝试加载该文件。我最初使用其中一个应用程序模板生成了应用程序并包含了Core Data。注意:我已经镜像了Client实体并生成了相应的Model类。

我已进入我的“应用程序委托”类并修改了persistentStoreCoordinator方法以引用我的“Testing.sqlite”文件,即

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Testing.sqlite"]];

我还将保存的桌面应用程序文件复制到了预期的位置,即

~/Library/Application Support/IPhone Simulator/User/... etc.

所以现在理论上至少两个应用程序中的每一个应该是相同的。

然而,当我试图从中加载数据时,它似乎总是空的。我的代码看起来有点像这样:

    // fetch the delegate.
TestingAppDelegate *app = (TestingAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [app managedObjectContext];

// construct the request.
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:managedObjectContext]; 
[request setEntity:entity]; 

// execute the request.
NSError *error;
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];

if (results == nil) { 
    // Handle the error.        
    NSLog(@"No data loaded");
} 

NSLog(@"Returned: %@", results);    

// finally release  
[results release]; 
[request release]; 

我似乎无法弄清楚出了什么问题。任何提示或建议将完全赞赏。

当我查看persistanceStoreCoordinator的实例,managedObjectContext,生成的数组(NSArray),同时调试我可以看到它似乎包含所有这些的0条记录。所以我很困惑。

注意:Testing.sqlite文件包含条目。

提前致谢, 马特

1 个答案:

答案 0 :(得分:0)

使用Core Data时,简单地复制sqlite数据库文件将不起作用。 您需要完全复制在桌面应用程序中创建的持久性存储。

但是,这可能是一个问题,即即使您复制了Core Data,Core Data也没有看到您的数据库文件。请尝试以下方法:

1)将您的数据库文件添加到资源组中的项目中 2)使用此方法实际复制数据库文件

- (NSString *) initialize_db {
    NSString *DATABASE_RESOURCE_NAME = @"testing";
    NSString *DATABASE_RESOURCE_TYPE = @"sqlite";
    NSString *DATABASE_FILE_NAME = @"testing.sqlite";

    // copy the database from the bundle if necessary
    // look to see if DB is in known location (~/Documents/$DATABASE_FILE_NAME)


    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentFolderPath = [searchPaths objectAtIndex: 0];
        NSString *dbFilePath = [documentFolderPath stringByAppendingPathComponent: DATABASE_FILE_NAME];
        [dbFilePath retain];

        if (! [[NSFileManager defaultManager] fileExistsAtPath: dbFilePath]) {
            // didn't find db, need to copy
            NSString *backupDbPath = [[NSBundle mainBundle]
                                      pathForResource:DATABASE_RESOURCE_NAME
                                      ofType:DATABASE_RESOURCE_TYPE];
            if (backupDbPath == nil) {
                // couldn't find backup db to copy, bail
                NSLog (@"couldn't init db");
                return NULL;
            } else {
                BOOL copiedBackupDb = [[NSFileManager defaultManager]
                                       copyItemAtPath:backupDbPath
                                       toPath:dbFilePath
                                       error:nil];
                if (! copiedBackupDb) {
                    // copying backup db failed, bail
                    NSLog (@"couldn't init db");
                    return NULL;
                }
            }
        }


        return dbFilePath;

    }

复制数据库文件后,会返回其文件路径,并使用它来实际打开数据库。

相关问题