【问题标题】:Mysterious EXC_BAD_ACCESS on dispatch_async *serial queue*dispatch_async *串行队列*上的神秘 EXC_BAD_ACCESS
【发布时间】:2016-02-12 00:55:32
【问题描述】:

我有一个基于位置的应用程序,它每 1 秒获取一次位置,并一次将一批位置保存到 CoreData 数据库,以免位置数组太大。但是,由于某种原因,即使我在 SERIAL QUEUE 上使用 dispatch_async,它也会因 EXC_BAD_ACCESS 而崩溃:

像这样创建了一个全局串行自定义队列:

var MyCustomQueue : dispatch_queue_t = 
dispatch_queue_create("uniqueID.for.custom.queue", DISPATCH_QUEUE_SERIAL);

在 didUpdateToLocation 协议函数中,如果批量大小大于预设数量,我有这段代码将最新一批 CLLocation 项保存到磁盘上:

if(myLocations.count == MAX_LOCATIONS_BUFFER)
{
    dispatch_async(MyCustomQueue)
    {

        for myLocation in self.myLocations
        {
        // the next line is where it crashes with EXC_BAD_ACCESS
            newLocation = NSEntityDescription.insertNewObjectForEntityForName(locationEntityName, inManagedObjectContext: context as NSManagedObjectContext) ;
            newLocation.setValue(..)
            // all the code to set the attributes
        }

        appDelegate.saveContext();

        dispatch_async(dispatch_get_main_queue()) {
        // once the latest batch is saved, empty the in-memory array
            self.myLocations = [];
        }

    }
}

神秘的是,即使您使用 dispatch_async (它只是使执行与主线程并发,不会触及 SQLite 数据),SERIAL QUEUE 是否应该按顺序执行其上的所有项目?

注意事项:

  • 坠机发生在随机时间...有时会在 0.5 英里后坠毁,有时会在 2 英里后坠毁,等等。
  • 如果我只是取出任何 dispatch_async 代码(只是让所有内容在主线程上按顺序执行),就没有问题。

【问题讨论】:

    标签: sqlite exc-bad-access dispatch-async


    【解决方案1】:

    您可以查看此链接。您的问题可能是因为您尝试将项目插入其中时没有 MOC。

    Core Data : inserting Objects crashed in global queue [ARC - iPhone simulator 6.1]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多