On Dec 7, 2010, at 11:13 AM, davel...@mac.com wrote:

> I've written a few small personal Mac apps and one iOS app and now am 
> attempting to write my first NSDocument-based Mac application. I would like 
> the document itself to be a SQLite file (with specific tables for my app - 
> not a generic SQLite file). I've been reading through the NSDocument 
> documentation and it appears this can be done, but I'm not 100% certain of 
> the steps and wanted to make certain I wasn't going down a path I shouldn't 
> before getting too far into it. Here is what I think I need to do:
> 
> Here's what I think I need to do in my NSDocument subclass.
> 
> When the user chooses a File->New, the method - (id)initWithType:(NSString 
> *)typeName error:(NSError **)outError is called so in it I would create a 
> SQLite database file in a temporary location and call setFileURL: to store 
> the location of that SQLite file (and store a connection to the SQLite 
> database file).
> 
> In the - (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName 
> error:(NSError **)outError, if the passed absoluteURL matches the return 
> value of fileURL:  I would do something like commit the SQLite transactions. 
> If the absoluteURL did not match the returned value of fileURL: I need to 
> copy the current SQLite file to this location. Will this work ok?
> 
> In the - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName 
> error:(NSError **)outError  I would just open the SQLite file and store my 
> connection to it for use in the other methods for updating the file.
> 
> Am I missing something? Is attempting to use a SQLite file as the document 
> file a bad idea?
> 
> I'm also considering an iOS application that uses the same SQLite database 
> files so that's why I'd like to use SQLite on the Mac if possible. I'm also 
> not ready to tackle using Core Data although that may be the better option if 
> there are reasons that SQLite doesn't map well to a NSDocument based 
> application. My other option is to not use the NSDocument architecture, and 
> just open one file at a time in my GUI but I thought I'd try to learn about 
> the NSDocument architecture while writing it.
> 
> Thanks,
> Dave

Nobody replied, but in case anyone else wants to try this, I think the best 
solution is to use the special sqlite :memory: database and load and save files 
between the file and the :memory: database. See 
<http://www.sqlite.org/backup.html>  In my case the sqlite files will never be 
more than 100KB so storing them in memory is a reasonable solution. Obviously 
if your files are much closer to the amount of RAM, this wouldn't be a good 
solution. 

If I run into issues farther along in the process with this approach, I'll post 
a follow-up to this thread, but as of now, I don't see any reason why it won't 
work.

Dave

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to