mardi 28 avril 2015

SQLite select statement not seeing prior write/insert using SQLite serialized mode on iOS

I am using SQLite with iOS and running into a problem where I am writing to the database, and the immediately reading the record that I just created. I am reading the record because I need to get the id created from the database in order to do subsequent writes. The problem I am running into is that the record that I just created with a write/insert is not there when I do a read (select). The select on the record works just fine in debug mode, and I can see it if I pause the thread, or do anything to slow down the code. I tried compiling with -DSQLITE_THREADSAFE=1 so that it SQLite runs in serialized mode and this seemed to work, but I got rejected from my app store submission because it was not working. It seems that the problem is that the code in the fully optimized release mode is running so fast that the insert does not complete before the read/select runs. I am not using any mult-threading, or async calls in my code when accessing the DB. The code where the problem is occurring is in a callback from a web service call, so that might be causing issues.

My question is: When using SQLite and doing a write and immediately reading that record (same thread, same block), is there something else that i should be doing to ensure the write is complete? I started using a serialized queue and GCD to hope that this works, but should I also be using transaction in SQLite to ensure the integrity of the db updates? If I use transactions, will it slow down my db calls?

Aucun commentaire:

Enregistrer un commentaire