title: How to debug/handle intermittent "authorization denied" and "disk i/o" errors when adding SQL store to an NSPersistentStoreCoordinator?
body: <p>I have an app in the app store and am using a logging service to get crash logs and associated log data. I am seeing an intermittent crash (low # of users affected and low # of crashes per user) but it is baffling me.</p>
  
  <p>What happens in these crashes is the following:</p>
  
  <ol>
  <li><p>App launches and initializes Core Data stack</p></li>
  <li><p>App attempts to add a SQL store to the NSPersistentStoreCoordinator with the following code (<code>storeURL</code> is valid):</p>
  
  <pre><code>NSDictionary *options = @{
      NSMigratePersistentStoresAutomaticallyOption : @(YES),
      NSInferMappingModelAutomaticallyOption : @(YES)
  };
  
  sqlStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                       configuration:nil
                                                                 URL:storeURL
                                                             options:options
                                                           error:&amp;error];
  </code></pre></li>
  <li><p>One of the following errors occur when adding this store:</p></li>
  </ol>
  
  <blockquote>
    <p>NSError:</p>
    
    <blockquote>
      <p>Domain=NSCocoaErrorDomain<br/>
      Code=256 "The operation couldn’t be completed. (Cocoa error 256.)"<br/>
      UserInfo=0x1dd946a0 {NSUnderlyingException=authorization denied, NSSQLiteErrorDomain=23}<br/></p>
    </blockquote>
  </blockquote>
  
  <p>or</p>
  
  <blockquote>
    <p>NSError:</p>
    
    <blockquote>
      <p>Domain=NSCocoaErrorDomain<br/>
      Code=256 "The operation couldn’t be completed. (Cocoa error 256.)"<br/>
      UserInfo=0xc6525d0 {NSUnderlyingException=disk I/O error, NSSQLiteErrorDomain=10}</p>
    </blockquote>
  </blockquote>
  
  <p>After this condition, the app will crash b/c the SQL store is required for the app to function. I could attempt to gracefully handle this failure by trying a new storeURL but I don't want the user to lose existing data. Also, I have never personally reproduced this issue and based on the low number of users affected and crash logs I believe it is a low impact problem and does not recur on a subsequent app launch.</p>
  
  <p>I'm hoping there's a Core Data guru out there with some suggestions on how to debug and prevent/handle these conditions. My core data stack initialization code is straight from the xcode project generator and I have ruled out any concurrency issues in that the persistent store coordinator is only initialized once (on launch) and this error occurs in this initialization.</p>
  
  <p>Happy to provide more code/info if relevant.</p>
  
  <p>thanks!</p>