0

I'm using FMDB to work with SQLite on my latest iOS project (supports iOS SDK 7.0 & above). Everything is working like a charm during development & I love this wrapper. I've tested on simulator & directly build to device (my device is iPhone 5C & iPad mini 2), no problems happened.

But things go down when I distribute an Ad-hoc build (using my developer certificate & provisioning) via TestFlight to my testers. They can download & install my app successfully but when it launch, an 'out of memory' error thrown out the screen.

I've checked some other SO questions & made sure every FMResultSet * is closed after used. But when I use iTools to explore the app's directory structure, surprisingly no database file was created, which mean FMDB can't open the SQLite database file and throw out the misleading 'out of memory' error. So what I've tried with the creation of database file:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appBundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
NSString *dbPath = [paths[0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.sqlite", appBundleName]];
_database = [FMDatabase databaseWithPath:dbPath];

<- what create foo-bar.sqlite inside the app's Documents folder

NSString *appBundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
_database = [FMDatabase databaseWithPath:[NSString stringWithFormat:@"/tmp/%@.sqlite", appBundleName]];

<- what create foo-bar.sqlite inside the tmp folder

_database = [FMDatabase databaseWithPath:@"/tmp/tmp.db"]];

<- what is exactly what FMDB tutorial shows

But no lucks. The .sqlite/.db file is still not created.

Anyone has faced this problem before or any suggestions to help me out of this stuck?

Thanks in advanced.

Thanh Nguyen
  • 161
  • 1
  • 10
  • 1
    `/tmp` is not a valid iOS path. You need a real path from inside your app's sandbox. And it needs to be a writable path which means not in your resource bundle. – rmaddy Jul 15 '14 at 01:49
  • @rmaddy I've tried with a path of Documents folder (what is inside my app's sandbox), but the sqlite file is still not created (see my first try above) – Thanh Nguyen Jul 15 '14 at 01:51
  • Hi, thanks for your quick reply. I found the problem. It is because when I create the database & tables, I did it in main thread, but when querying the tables, I do in background thread (dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{}) This lead to the misleading 'out of memory' error. So the question is, how can I query the tables in background thread? – Thanh Nguyen Jul 15 '14 at 09:12

1 Answers1

3

I believe you mentioned this on the github repo as well- but the basic answer is: use FMDatabaseQueue.

ccgus
  • 2,906
  • 23
  • 18