简体   繁体   English

使用FMDB将数据插入SQLite时,在dispatch_async上为EXC_BAD_ACCESS

[英]EXC_BAD_ACCESS on dispatch_async while inserting data into SQLite with FMDB

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. 我正在尝试将大量数据插入sqlite数据库。 It's 20000 rows of data, so I have to do it in the background thread. 它是20000行数据,所以我必须在后台线程中进行操作。

I have a NSObject .h and .m files to handle the database operations. 我有一个NSObject .h和.m文件来处理数据库操作。 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. 我试过用FMDatabaseQueue更改FMDatabase,但是没有运气。 So any help would be highly appreciated because I'm pretty desperate in this. 因此,任何帮助将不胜感激,因为我对此感到非常绝望。

Thanks. 谢谢。 Cheers! 干杯!

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. 如果您两次调用InsertDataIntoTable或任何其他尝试访问数据库的方法,则可能会遇到以下情况:在有时间执行查询之前,数据库连接已关闭。 Consider this scenario: 考虑这种情况:

  1. Thread 1 opens db connection 线程1打开数据库连接
  2. Thread 2 opens db connection 线程2打开数据库连接
  3. Thread 1 adds a block to queue 线程1将一个块添加到队列
  4. Thread 2 adds a block to queue 线程2将一个块添加到队列
  5. Thread 1 finish running and closes the db connection 线程1完成运行并关闭数据库连接
  6. Thread 2 tries to run his block, but the db connection is already closed. 线程2尝试运行他的块,但db连接已关闭。

Try to call [database open] inside the block. 尝试在块内调用[database open]

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. EXC_BAD_ACCESS表示您有一个僵尸,启用了僵尸对象并使用了工具工具,它将向您显示僵尸对象的位置。 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 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 在xCode中,屏幕顶部的工具栏Product-> Edit Scheme-> Diagnostics->单击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: 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 Edit3:图片

在此处输入图片说明

Then you should see this 那你应该看这个

在此处输入图片说明

Then when you run the app, navigate to the screen where the error occurs, and the app should stop, and this should appear 然后,当您运行该应用程序时,导航至发生错误的屏幕,并且该应用程序应停止运行,并出现

在此处输入图片说明

Click the arrow beside the error message, and it should you show in the stack trace where the error is occuring 单击错误消息旁边的箭头,它应该在堆栈跟踪中显示发生错误的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM