NSPersistentDocument: SQLite error 21 on save
NSPersistentDocument: SQLite error 21 on save
- Subject: NSPersistentDocument: SQLite error 21 on save
- From: Fritz Anderson <email@hidden>
- Date: Sat, 06 Aug 2011 16:28:44 -0500
Lion SDK.
I have an NSPersistentDocument, with autosave-in-place enabled. When I try to save the document after editing it, it balks with both application-modal alerts and document-modal sheets, all with generic messages about being unable to create or save the document.
I set "-com.apple.CoreData.SQLDebug 1" in the command-line arguments, and got this when I pressed cmd-S on my edited document (stripped of time stamps, and pathnames shortened):
=======================
CoreData: annotation: Connecting to sqlite database file at ".../post-cmd-T.prleague"
CoreData: annotation: creating schema.
CoreData: sql: pragma page_size=4096
CoreData: sql: pragma auto_vacuum=2
CoreData: annotation: Disconnecting from sqlite database due to an error.
CoreData: error: (21) I/O error for database at .../post-cmd-T.prleague. SQLite error code:21, 'unable to open database file'
CoreData: annotation: Disconnecting from sqlite database.
=======================
The sqlite.org result-code page says:
#define SQLITE_MISUSE 21 /* Library used incorrectly */
which is disquieting.
When I create one of my documents and immediately save it in an XML store, there is no problem. I can edit and resave with no problems.
The SQLite problem is irregular:
* If I save the document when it's new, the error usually doesn't appear, but it may.
* If I don't save it until after adding about 13,000 records to it, the error may or may not occur.
* If I change an attribute of one of those records, the error always appears.
If the error occurs on the first save, a zero-length file is created at the desired location. If the second save (programmatically-filled DB) succeeds, but the third fails, the file on disk seems to be in good shape: Navicat for SQLite Lite reads the database, the contents look correct, and if I edit the database in Navicat and reopen it with my app, the Navicat edit shows up.
The changed-attribute edit makes the save fail regardless of whether it's done through a binding to a table cell, or programmatically in response to a menu command:
=======================
- (IBAction) changeName: (id) sender
{
id selection = [self.teamArrayController selection];
NSString * oldName = [selection valueForKey: @"teamName"];
if (oldName != NSNoSelectionMarker) {
NSString * newName = [oldName stringByAppendingString: @" CHANGED"];
[selection setValue: newName forKey: @"teamName"];
}
}
=======================
The object in question is an instance of an NSManagedObject subclass. I've verified that the data model points its entity at the right class, and the object otherwise behaves as expected. The teamName of the object as shown in the table changes as expected in response to -changeName:.
This looks bad, but I'm willing to believe I'm doing something wrong. I am willing to go into details and share code, but there's a lot of it, and I am hoping that this will sound familiar to someone.
Any suggestions?
— F
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden