在Core data持久存储中传送静态(只读)数据的正确方法是什么?

时间:2022-12-08 11:56:44

I want to ship static read-only data for use in my Core Data model. The problem is that there are obviously different persistent store types and I don't know if the format of those types is supposed to be opaque or if I'm supposed to be able to construct them by hand.

我希望在Core data模型中使用静态只读数据。问题是,显然有不同的持久存储类型,我不知道这些类型的格式应该是不透明的,还是应该能够手工构造它们。

Right now I just have a plist and it's very small (maybe 30 entries total).

现在我只有一个plist,它很小(可能总共有30个)。

Should I just write code to import the plist into my data store when the app is first installed, or is there some way I can ship a hand-constructed initial version of the data store file?

当应用程序第一次安装时,我是否应该编写代码将plist导入到我的数据存储中,或者是否有某种方法可以将手工构建的数据存储文件的初始版本发送出去?

(I'm using the default sqlite persistent store.)

(我使用的是默认的sqlite持久存储。)

3 个解决方案

#1


3  

I would not try to hand-construct it, but you certainly should execute an import and save a final Core Data SQLite file to ship with your app.

我不会尝试手工构建它,但是您确实应该执行导入,并保存最终的核心数据SQLite文件,以便与您的应用程序一起使用。

I plan to write a small mac utility (using the same data model) to generate the Core Data SQLite file for my iPhone app (the import is actually from a web server). Then, I will add the file that was persisted by the utility into my iPhone app's project.

我计划编写一个小的mac实用程序(使用相同的数据模型)来为我的iPhone应用程序生成核心数据SQLite文件(导入实际上来自web服务器)。然后,我将实用程序保存的文件添加到我的iPhone应用程序的项目中。

#2


1  

To add a bit to the answer to my own question, I noticed that the Recipes sample code application comes with a default sqlite backing store:

为了给我自己的问题添加一点答案,我注意到菜谱示例代码应用程序附带一个默认的sqlite支持存储:

// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
    NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Recipes" ofType:@"sqlite"];
    if (defaultStorePath) {
        [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
    }
}

But then again, for another purpose it comes with some static read-only data in a plist file! (TemperatureData.plist) So go figure....

但是,为了另一个目的,它在plist文件中附带了一些静态只读数据!(TemperatureData.plist)所以去图....

#3


0  

Not many people know this, but you can actually use core data on OSX and use that store file then on the iOS. So essentially write some code that uses exactly the same schema and your model objects (they should all compile and work on OSX).

没有多少人知道这一点,但是你可以在OSX上使用核心数据,然后在iOS上使用存储文件。因此本质上,编写一些使用相同模式和模型对象的代码(它们都应该编译并使用OSX)。

OSX development isn't really that hard to get your handle on, if you know iOS SDK :-)

如果你知道iOS SDK的话,OSX开发并不是很难搞定的。

HTH

HTH

#1


3  

I would not try to hand-construct it, but you certainly should execute an import and save a final Core Data SQLite file to ship with your app.

我不会尝试手工构建它,但是您确实应该执行导入,并保存最终的核心数据SQLite文件,以便与您的应用程序一起使用。

I plan to write a small mac utility (using the same data model) to generate the Core Data SQLite file for my iPhone app (the import is actually from a web server). Then, I will add the file that was persisted by the utility into my iPhone app's project.

我计划编写一个小的mac实用程序(使用相同的数据模型)来为我的iPhone应用程序生成核心数据SQLite文件(导入实际上来自web服务器)。然后,我将实用程序保存的文件添加到我的iPhone应用程序的项目中。

#2


1  

To add a bit to the answer to my own question, I noticed that the Recipes sample code application comes with a default sqlite backing store:

为了给我自己的问题添加一点答案,我注意到菜谱示例代码应用程序附带一个默认的sqlite支持存储:

// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
    NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Recipes" ofType:@"sqlite"];
    if (defaultStorePath) {
        [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
    }
}

But then again, for another purpose it comes with some static read-only data in a plist file! (TemperatureData.plist) So go figure....

但是,为了另一个目的,它在plist文件中附带了一些静态只读数据!(TemperatureData.plist)所以去图....

#3


0  

Not many people know this, but you can actually use core data on OSX and use that store file then on the iOS. So essentially write some code that uses exactly the same schema and your model objects (they should all compile and work on OSX).

没有多少人知道这一点,但是你可以在OSX上使用核心数据,然后在iOS上使用存储文件。因此本质上,编写一些使用相同模式和模型对象的代码(它们都应该编译并使用OSX)。

OSX development isn't really that hard to get your handle on, if you know iOS SDK :-)

如果你知道iOS SDK的话,OSX开发并不是很难搞定的。

HTH

HTH