2

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!

tyegah123
  • 197
  • 3
  • 13
  • tyegah123 post your stack error in order to help you. Seems you made a call on a free memory block – Mr Bonjour Mar 19 '13 at 08:22
  • Did you enable zombies already? – Hermann Klecker Mar 19 '13 at 08:43
  • @MrBonjour : yeah, I was thinking of posting the image but the stack error keeps changing from error code 1 to 2 and so on. @ HermannKlecker : Yeah I enabled zombie, and nothing came out on the console log. It's kind of weird. – tyegah123 Mar 19 '13 at 11:07

2 Answers2

1

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.

Eli Ganem
  • 1,479
  • 1
  • 13
  • 26
0

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

AdamM
  • 4,400
  • 5
  • 49
  • 95
  • well,first of all thank you for giving me an answer. But I'm sorry if this might sound like a dumb question, but what is a zombie? and how to enable that? – tyegah123 Mar 19 '13 at 10:06
  • 1
    A Zombie is the opposite of a memory leak in which the object has been released, but it is being kept alive by a strong reference to it by another object, hence the name zombie. See my edit about how to enable it – AdamM Mar 19 '13 at 10:37
  • Okay thanks. I enabled it but nothing came out on my console log. I wonder why. – tyegah123 Mar 19 '13 at 11:10
  • Did you click product-profile, and select zombie trace. If so, when the error appears, an error message should appear saying zombie found at, and you can click the arrow and it will take you to the where the error is, will update answer with pics – AdamM Mar 19 '13 at 11:43
  • Sorry but that's a horrible answer to this particular question. 'EXC_BAD_ACCESS' can be many things that Zombie instruments won't see. In this instance, it's most likely a concurrence issue with sqlite. – Dave Aug 26 '13 at 04:14