2016-02-12 61 views
1

我有一個基於位置的應用程序,它每隔1秒就會獲取一個位置,並且一次將一批位置保存到CoreData DB中,以避免位置數組過大。然而,由於某種原因,與EXC_BAD_ACCESS崩潰,即使我用dispatch_async上的串行隊列: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 = []; 
     } 

    } 
} 

神祕之處,是不應該在執行上的所有項目一個串行隊列順序,即使你使用d ispatch_async(它只是讓執行與主線程併發,並不接觸SQLite數據)?

注:

  • 的崩潰發生在隨機時間......有時它會後0.5英里,其他時間後2英里崩潰等
  • 如果我只是拿出任何的dispatch_async代碼(只需在主線程上按順序執行),沒有問題。

回答