这是系列教程的第二部分,有助于你加快掌握基本的Core Data内容。
在系列教程一中,我们为对象建立了可视化数据模型,运行了快速肮脏测试并勾在一个表视图(table view)中来显示。而在这个教程,我们将讨论如何把已有的数据导入或者预先载入到Core Data里面,这样我们的应用开始时会有一些好的默认数据。
在系列教程的最后部分,我们将讨论如何使用NSFetchedResultsController来优化应用,降低内存开销和提升响应时间。
Preloading / Importing Existing Data
预载/导入已有数据
我们要如何在Core Data中预先载入数据呢?流行的解决方案有两种:
- 在sqlite数据库中预先填充。对此我们可以Core Data在模型基础上建立数据库结构,然后使用工具来填入数据库。这类工具可以是基于Core Data API的Mac或者iPhone应用,或是一些直接填入sqlite数据库的程序。一旦数据库填好了,只需将其包含到应用里面作为默认数据库,在不存在已有的数据库情况下。
我们将会采用第二种方式,因为它更简单更有效。为了填充数据库,我们只需稍微扩展一下已有的Python脚本。
注意到为什么用Python脚本而不是用基于Core Data API的工具来来导入数据,是因为我们现在有点走底层的样子,将来可能会容易损坏…但是对于本教程我认为第一,由于我们刚刚接触了sqlite,这样对学习的经验巩固更好,而且对事情的进展看的更加清楚;第二,更简单!
所以,让我们拿出工程中产生的sqlite数据库的拷贝。找出相关文件的最简单的方法就是在程序委托(application delegate)中persistentStoreCoordinator函数里面storeUrl一行下方设置一个断点。你可以通过检测storeUrl变量的内容来获取sqlite数据库备份文件的完整路径。找到它并拷到你的Python脚本目录。
一旦你找到数据库,使用sqlite3来简单看看数据库的模样:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
sqlite3> .
schema
CREATE
TABLE
ZFailedBANKDETAILS ( Z_PK
INTEGER
PRIMARY
KEY
,Z_ENT
INTEGER
sql plain" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,
@H_301_152@
Z_OPT
sql keyword" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,ZZIP
sql keyword" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,ZINFO
sql keyword" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,ZUPDATEDDATE
TIMESTAMP
sql plain" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,
ZCLOSEDATE
);
ZFailedBANKINFO ( Z_PK
sql keyword" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,
sql keyword" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,ZNAME
VARCHAR
sql plain" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,ZSTATE
sql keyword" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,
ZCITY
);
Z_MetaDATA (Z_VERSION
sql keyword" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,
Z_UUID
(255),Z_PLIST BLOB);
Z_PRIMARYKEY (Z_ENT
@H_404_198@Z_SUPER
sql keyword" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,Z_NAME
sql keyword" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,
sql keyword" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:0px!important; padding-bottom:0px!important; padding-left:0px!important; border-top-width:0px!important; border-right-width:0px!important; border-bottom-width:0px!important; border-left-width:0px!important; border-style:initial; border-color:initial; outline-width:0px!important; outline-style:initial!important; outline-color:initial!important; font-size:1em!important; vertical-align:baseline!important; background-color:initial!important; border-style:initial!important; border-color:initial!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow-x:visible!important; overflow-y:visible!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; width:auto!important; font-family:Consolas,Z_MAX
);
1|2|1|1|Test Bank|Testland|Testville
2|2|1|2|Test Bank|Testland|Testville
3|2|1|3|Test Bank|Testland|Testville
ZFailedBANKDETAILS;
1|1|1|12345|1|292794835.855615|292794835.679693
2|1|1|12345|2|292794875.943392|292794875.768675
3|1|1|12345|3|292795809.375025|292795809.215297
Z_PRIMARYKEY;
1|FailedBankDetails|0|3
2|FailedBankInfo|0|3
|
这里有段快速描述。Z_MetaDATA包含了一些关于Core Data需要实现的模型信息。Z_PRIMARYKEY包含了(在其他东西里面)各个实体所用到的最大key。
至于ZFailedBANKINFO跟ZFailedBANKDETAILS,这些是我们的主要数据表。Z_PK是各个表的唯一id,Z_ENT是他们的实体id(跟Z_PRIMARYKEY表中列出的一样),最后那些是我们的普通字段。