SQLite Persistent Objects

Wouldn't it be nice if your Objective-C data objects just knew how to save and load themselves? It'd be nice if you could just call "save" and trust that your object would save itself properly somewhere, and that when you wanted to load it back in, you could just call a class method to retrieve the object or objects you wanted?

Marek's comments

The wardrive today failed abysmally thanks to sqlite persistant objects being more shit than I could ever have imagined. It kept crashing between 100-150 aps (I tried from 0 to over 100 five separate times). When I got back home I tried adding 2000 aps to the db and it works great on the simulator. However, check out what happens on the device...

2008-09-14 22:06:47.819 WiFiFoFum[581:20b] 0
2008-09-14 22:06:47.998 WiFiFoFum[581:20b] 1
2008-09-14 22:06:48.169 WiFiFoFum[581:20b] 2
2008-09-14 22:06:48.332 WiFiFoFum[581:20b] 3
2008-09-14 22:08:05.762 WiFiFoFum[581:20b] 515
2008-09-14 22:08:07.756 WiFiFoFum[581:20b] 516
2008-09-14 22:08:10.205 WiFiFoFum[581:20b] 517
2008-09-14 22:08:12.520 WiFiFoFum[581:20b] 518
2008-09-14 22:08:15.283 WiFiFoFum[581:20b] 519
2008-09-14 22:08:18.319 WiFiFoFum[581:20b] 520

The Debugger has exited due to signal 10 (SIGBUS).The Debugger has exited due to signal 10 (SIGBUS).

Look at the times, nearly 2 minutes just to add 500 APs! Plus, at 500APs it's taking 3 seconds to add a single new AP to the DB!!! Insanely poor performance. I should have realised earlier because when it was inserting a single scan into even an empty db I had to shunt the db updates into a background thread because it was causing the UI to lock up for 500ms.

Worse, the exit after 520 was a crash due to no memory being free on the device. It looks like SQLite uses a memory mapping thing to cache the DB contents so not only does the speed of the db degrade substantially as more items go in, but the device runs out of RAM too.

So, after much, much testing over the last couple of days and another 5 hours just there I've decided sqlite persistent objects is buggy as hell and the performance is completely dire and unusable. I decided to try saving files in a directory to see how that would perform and compared to sqlite it's fucking instant. Actually, not compared to anything... it just is fucking instant. Inserting a new file when there are already 2000 in is instant. Counting the number of objects in the directory is instant (I tested up to 5000). I was even able to pull it back on to the main thread cause it's instant. So, for just now until sqlite sees some massive improvements it looks like the files are the way to go.

- marek