简体   繁体   English

数据未保存到核心数据

[英]Data not being saved to core data

I am using Xcode 5 compiling exclusively for iOS 7. 我正在使用专为iOS 7编译的Xcode 5。

I am reading data from a UTF8 txt file to populate a core data entity. 我正在从UTF8 txt文件中读取数据以填充核心数据实体。 After reading the data, I log it to the console. 读完数据后,我将其记录到控制台。 The data is there. 数据就在那里。 I populate the entity and log the entity to the console. 我填充实体并将实体记录到控制台。 I save it. 我救了它。 No error. 没错。 No crash. 没有崩溃。

I see 3 files on the device: 我在设备上看到3个文件:

  • MyDatabase.sqlite MyDatabase.sqlite
  • MyDatabase.sqlite-shm MyDatabase.sqlite-SHM
  • MyDatabase.sqlite-wal MyDatabase.sqlite,沃尔玛

When the app starts and MyDatabase.sqlite is created empty it has 40 kb. 当应用程序启动并且MyDatabase.sqlite创建为空时,它有40 kb。 At this point the shm file is 32kb and the wal file is zero. 此时shm文件为32kb, wal文件为零。

After I write the data to the database, the wal files grows to 1,7 Mb but the other two files keep their initial sizes or in other words, the data is not being saved to the database. 在将数据写入数据库之后, wal文件增长到1,7 Mb,但其他两个文件保持其初始大小,换句话说,数据未保存到数据库。 I have confirmed that by inspecting the sqlite file with an external database viewer. 我通过使用外部数据库查看器检查sqlite文件来确认。

This is the code I am using: 这是我正在使用的代码:

// NSArray *arrayOfYears = ... 
// this contains an array of numbers read from the CSV file
// at this point the array contains numbers in text format

// NSArray *arrayOfBrands = ...
// this is an array of brands

for (int i=0; i<[arrayOfBrands count]; i++) {

            Cars *car = [NSEntityDescription insertNewObjectForEntityForName:@"Cars" inManagedObjectContext:context];

            car.brand = [arrayOfBrands objectAtIndex:i];
            car.year = [NSNumber numberWithInt:[[arrayOfYears objectAtIndex:i] integerValue]];

            NSError *error = nil;
            if (![context save:&error]) {
                // Handle the error.
                NSLog(@"error = %@", error);
            }
}

This produces no error or crash. 这不会产生错误或崩溃。 It goes like it was saving. 它就像是在储蓄。 I have increased mySql debug level to 3 and this is what I see on console... 我已将mySql调试级别增加到3,这是我在控制台上看到的...

when the table is created empty 当表创建为空时

CoreData: annotation: Connecting to sqlite database file at "/var/mobile/Applications/BB22334C4-550A-4C44-B17A-3F02062EC687/Documents/MyDatabase.sqlite"
CoreData: annotation: creating schema.
CoreData: sql: pragma page_size=4096
CoreData: sql: pragma auto_vacuum=2
CoreData: sql: BEGIN EXCLUSIVE
CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'
CoreData: sql: CREATE TABLE ZCARS ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZYEAR INTEGER, ZBRAND VARCHAR ) 
CoreData: annotation: Creating primary key table.
CoreData: sql: CREATE TABLE Z_PRIMARYKEY (Z_ENT INTEGER PRIMARY KEY, Z_NAME VARCHAR, Z_SUPER INTEGER, Z_MAX INTEGER)
CoreData: sql: INSERT INTO Z_PRIMARYKEY(Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES(1, 'Cars', 0, 0)
CoreData: sql: CREATE TABLE Z_METADATA (Z_VERSION INTEGER PRIMARY KEY, Z_UUID VARCHAR(255), Z_PLIST BLOB)
CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'
CoreData: sql: DELETE FROM Z_METADATA WHERE Z_VERSION = ?
CoreData: details: SQLite bind[0] = 1
CoreData: sql: INSERT INTO Z_METADATA (Z_VERSION, Z_UUID, Z_PLIST) VALUES (?, ?, ?)
CoreData: details: SQLite bind[0] = 1
CoreData: details: SQLite bind[1] = "C6E2268B-6792-4298-B292-5025E5BDE31A"
CoreData: details: SQLite bind[2] = <NSData len=455>
CoreData: annotation: Saving new meta data; version = 1 ; UUID = C6E2268B-6792-4298-B292-5025E5BDE31A
CoreData: sql: COMMIT
CoreData: sql: pragma journal_mode=wal
CoreData: sql: pragma journal_mode=wal
CoreData: sql: pragma cache_size=200
CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADATA

after saving one brand/year 保存一个品牌/年后

CoreData: sql: BEGIN EXCLUSIVE
CoreData: sql: SELECT Z_MAX FROM Z_PRIMARYKEY WHERE Z_ENT = ?
CoreData: annotation: getting max pk for entityID = 4
CoreData: sql: UPDATE Z_PRIMARYKEY SET Z_MAX = ? WHERE Z_ENT = ? AND Z_MAX = ?
CoreData: annotation: updating max pk for entityID = 4 with old = 0 and new = 1
CoreData: sql: COMMIT
CoreData: sql: BEGIN EXCLUSIVE
CoreData: sql: INSERT INTO ZTABULEIRO(Z_PK, Z_ENT, Z_OPT, ZYEAR, ZBRAND) VALUES(?, ?, ?, ?, ?)
CoreData: details: SQLite bind[0] = (int64)1
CoreData: details: SQLite bind[1] = (int64)4
CoreData: details: SQLite bind[2] = (int64)1
CoreData: details: SQLite bind[3] = "FORD"
CoreData: details: SQLite bind[4] = (int64)1989
CoreData: sql: COMMIT
CoreData: annotation: Changing objectID 0x14e71730 <x-coredata:///Cars/t38FA86EE-4124-4FD7-A8C0-8CE7BBAC73782> to 0x14e73ef0 <x-coredata://C6E3368B-6792-4298-B292-5025E5BDE31A/Cars/p1>
CoreData: sql: pragma page_count
CoreData: annotation: sql execution time: 0.0014s
CoreData: sql: pragma freelist_count
CoreData: annotation: sql execution time: 0.0016s

one strange thing I see here is the year variable like int64 when I have defined it like integer 16. Why it is using integer 64 is beyond me... 我在这里看到的一个奇怪的事情是像int64这样的年变量,当我将它定义为整数16时。为什么它使用整数64超出了我...

The data is not being saved at all. 根本没有保存数据。 The data is there. 数据就在那里。 If I log the arrays of data to console I see the data. 如果我将数据数组记录到控制台,我会看到数据。 If I log the entities populated before saving I see the data. 如果我在保存之前记录填充的实体,我会看到数据。

I am testing this on a device running iOS 7. Thanks. 我在运行iOS 7的设备上测试这个。谢谢。

What am I missing? 我错过了什么? How do I debug that? 我该如何调试?

The data is being saved to the database if the write-ahead-log (wal) is increasing in size, See documentation here http://www.sqlite.org/draft/wal.html . 如果预写日志(wal)的大小正在增加,则数据将保存到数据库中,请参阅http://www.sqlite.org/draft/wal.html中的文档。 What are you using to open the database file? 你用什么打开数据库文件? You must use a tool that works with WAL journal mode. 您必须使用适用于WAL日志模式的工具。 Rather than looking at the db files directly, try creating a new context and fetching your saved entities. 不是直接查看db文件,而是尝试创建新上下文并获取已保存的实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM