Question

So, I've read through every posts I found on internet but i still can't seem to make this work.

I'm trying to insert a huge amount of data into sqlite database. It's 20000 rows of data, so I have to do it in the background thread.

I have a NSObject .h and .m files to handle the database operations. And I call them from inside my main view.

Here's my code :

SQLiteDBHandler.m :

 database = [FMDatabase databaseWithPath:[self getDBPath]];
    [database open];
    dispatch_queue_t q = dispatch_queue_create("FMDBQueue", NULL);
    dispatch_async(q, ^{
        for(Customer *c in arrayOfObjects) {
            [database executeUpdate:@"INSERT INTO SNP(rdis, Position, FirstName, LastName) VALUES (?,?,?,?)", c.ID, c.Position, c.FirstName, c.LastName];
        }
        [database close];
    });

and for calling the method in the main view, I call it this way :

SQLiteDBHandler *dbHandler = [[SQLiteDBHandler alloc]init];

[dbHandler insertDataIntoTable:mutableArray];

I've tried changing the FMDatabase with FMDatabaseQueue with no luck. So any help would be highly appreciated because I'm pretty desperate in this.

Thanks. Cheers!

Was it helpful?

Solution 2

EXC_BAD_ACCESS means you have a zombie, enable zombie objects and use the instruments tool and it will show you where the zombie object is. This link here is a good tutorial on how to detect them

http://www.raywenderlich.com/2696/how-to-debug-memory-leaks-with-xcode-and-instruments-tutorial

Hope that helps

EDIT:

In xCode, toolbar at top of the screen Product->Edit Scheme->Diagnostics-> Click Enable Zombie Objects

Then select product on the toolbar at top of the screen, and go down to profile, and instruments should appear, and you should see an option for zombie, select that.

Edit2:

Forgot to say, the zombie template will only appear if you profiling on the simulator, it won't appear if you try profiling on an actual device

Edit3: Pics

enter image description here

Then you should see this

enter image description here

Then when you run the app, navigate to the screen where the error occurs, and the app should stop, and this should appear

enter image description here

Click the arrow beside the error message, and it should you show in the stack trace where the error is occuring

OTHER TIPS

If you call InsertDataIntoTable twice, or any other method that tries to access the database, you might get a situation where the database connection is closed before you have time to execute your query. Consider this scenario:

  1. Thread 1 opens db connection
  2. Thread 2 opens db connection
  3. Thread 1 adds a block to queue
  4. Thread 2 adds a block to queue
  5. Thread 1 finish running and closes the db connection
  6. Thread 2 tries to run his block, but the db connection is already closed.

Try to call [database open] inside the block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top