title: core data sqlite file is corrupted
body: <p>I'm working on an app that on logout I'm deleting the sqlite file and upon log in I'm creating a new one but after logging back in and while getting the data from the server I'm encountering a mystery issue that I could not fix since 2 weeks:</p>
  
  <pre><code>2013-04-29 07:05:09.107 APP[85958:1cd03] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZACTION, t0.ZDISPLAYNAME, t0.ZENTITYNAMELIST, t0.ZFIELDCONSULTANTID, t0.ZKEY, t0.ZLASTUPDATETIME, t0.ZMESSAGE, t0.ZPATH, t0.ZPERIODMONTH, t0.ZPERIODYEAR, t0.ZREQUESTDURATION, t0.ZREQUESTNAME, t0.ZREQUESTSTARTTIME, t0.ZSTATE, t0.ZSTOREID, t0.ZTERRITORYID, t0.ZUPDATEDURATION, t0.ZUPDATESTARTTIME FROM ZSYNCREQUESTENTITY t0 WHERE  t0.ZKEY = ?  LIMIT 1 
  2013-04-29 07:05:09.107 APP[85958:1cd03] CoreData: details: SQLite bind[0] = "TERRITORY_DATA" 
  2013-04-29 07:05:09.108 APP[85958:1cd03] CoreData: annotation: Disconnecting from sqlite database due to an error. 
  2013-04-29 07:05:09.166 APP[85958:1cd03] CoreData: error: (26) Fatal error.  The database at /Users/admin/Library/Application Support/iPhone Simulator/6.0/Applications/C01FFF00-8530-4C8E-B954-AD9A70149972/Library/Application Support/APP/APP.sqlite is corrupted.  SQLite error code:26, 'file is encrypted or is not a database'
  </code></pre>
  
  <p>I'm using MagicalRecord third party to handle merging of context in background. I'm not sure if it can be the cause of the issue</p>
  
  <p>any help would be appreciated ?</p>
  
  <p>Thanks.</p>
  
  <p>EDITED:</p>
  
  <p>here is the stacktrace of 2 threads that might help to track the issue</p>
  
  <pre><code>thread #1: tid = 0x1f03, 0x02228e52 libobjc.A.dylib`objc_exception_throw, stop reason = breakpoint 1.1
      frame #0: 0x02228e52 libobjc.A.dylib`objc_exception_throw
      frame #1: 0x0069d772 CoreData`_execute + 1634
      frame #2: 0x0069d011 CoreData`-[NSSQLiteConnection execute] + 2801
      frame #3: 0x006b15ce CoreData`-[NSSQLChannel selectRowsWithStatement:] + 94
      frame #4: 0x006ba28f CoreData`newFetchedRowsForFetchPlan_MT + 1279
      frame #5: 0x006a56e3 CoreData`-[NSSQLCore newRowsForFetchPlan:] + 323
      frame #6: 0x006a4e07 CoreData`-[NSSQLCore objectsForFetchRequest:inContext:] + 711
      frame #7: 0x006a48f4 CoreData`-[NSSQLCore executeRequest:withContext:error:] + 404
      frame #8: 0x006a3a6d CoreData`-[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 2445
      frame #9: 0x006a19c9 CoreData`-[NSManagedObjectContext executeFetchRequest:error:] + 569
      frame #10: 0x006f4276 CoreData`-[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:] + 502
      frame #11: 0x0076e155 CoreData`__82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke_0 + 677
      frame #12: 0x006f4071 CoreData`internalBlockToNSManagedObjectContextPerform + 17
      frame #13: 0x03d9a014 libdispatch.dylib`_dispatch_client_callout + 14
      frame #14: 0x03d89d5f libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 58
      frame #15: 0x03d89aa3 libdispatch.dylib`dispatch_barrier_sync_f + 108
      frame #16: 0x006f403b CoreData`_perform + 187
      frame #17: 0x006f3e9e CoreData`-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] + 238
      frame #18: 0x006a19c9 CoreData`-[NSManagedObjectContext executeFetchRequest:error:] + 569
      frame #19: 0x006f4276 CoreData`-[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:] + 502
      frame #20: 0x0076e155 CoreData`__82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke_0 + 677
      frame #21: 0x006f4071 CoreData`internalBlockToNSManagedObjectContextPerform + 17
      frame #22: 0x03d8b731 libdispatch.dylib`_dispatch_barrier_sync_f_slow_invoke + 89
      frame #23: 0x03d9a014 libdispatch.dylib`_dispatch_client_callout + 14
      frame #24: 0x03d8a7d5 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 296
      frame #25: 0x03565af5 CoreFoundation`__CFRunLoopRun + 1925
      frame #26: 0x03564f44 CoreFoundation`CFRunLoopRunSpecific + 276
      frame #27: 0x03564e1b CoreFoundation`CFRunLoopRunInMode + 123
      frame #28: 0x02d6e7e3 GraphicsServices`GSEventRunModal + 88
      frame #29: 0x02d6e668 GraphicsServices`GSEventRun + 104
      frame #30: 0x0117065c UIKit`UIApplicationMain + 1211
      frame #31: 0x000030ad SubwayDA`main + 125 at main.m:12
  
  thread #29: tid = 0x4103, 0x986d6c5e libsystem_kernel.dylib`semaphore_wait_trap + 10
          frame #0: 0x986d6c5e libsystem_kernel.dylib`semaphore_wait_trap + 10
          frame #1: 0x03d8bf08 libdispatch.dylib`_dispatch_thread_semaphore_wait + 16
          frame #2: 0x03d89b3a libdispatch.dylib`_dispatch_barrier_sync_f_slow + 149
          frame #3: 0x03d89a5c libdispatch.dylib`dispatch_barrier_sync_f + 37
          frame #4: 0x006f403b CoreData`_perform + 187
          frame #5: 0x006f3e9e CoreData`-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] + 238
      frame #6: 0x006a19c9 CoreData`-[NSManagedObjectContext executeFetchRequest:error:] + 569
      frame #7: 0x00236fbb APP`__67+[NSManagedObject(MagicalRecord) MR_executeFetchRequest:inContext:]_block_invoke_0 + 251 at NSManagedObject+MagicalRecord.m:33
      frame #8: 0x006f3d3f CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 95
      frame #9: 0x03d9a014 libdispatch.dylib`_dispatch_client_callout + 14
      frame #10: 0x03d89d5f libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 58
      frame #11: 0x03d89aa3 libdispatch.dylib`dispatch_barrier_sync_f + 108
      frame #12: 0x006f3cba CoreData`-[NSManagedObjectContext performBlockAndWait:] + 202
      frame #13: 0x00236d9d APP`+[NSManagedObject(MagicalRecord) MR_executeFetchRequest:inContext:] + 285 at NSManagedObject+MagicalRecord.m:29
      frame #14: 0x002372ed APP`+[NSManagedObject(MagicalRecord) MR_executeFetchRequestAndReturnFirstObject:inContext:] + 141 at NSManagedObject+MagicalRecord.m:53
      frame #15: 0x002351a6 APP`+[NSManagedObject(MagicalFinders) MR_findFirstWithPredicate:inContext:] + 150 at NSManagedObject+MagicalFinders.m:128
      frame #16: 0x002350d0 APP`+[NSManagedObject(MagicalFinders) MR_findFirstWithPredicate:] + 128 at NSManagedObject+MagicalFinders.m:121
      frame #17: 0x001f2a3d APP`-[SyncRequestOperation updateSyncRequestFromItem:] + 173 at SyncRequestOperation.m:148
      frame #18: 0x001f2c91 APP`-[SyncRequestOperation updateSyncRequestFromItemOnCurrentThread] + 97 at SyncRequestOperation.m:181
      frame #19: 0x001f4033 APP`-[SyncRequestOperation loadEntities] + 355 at SyncRequestOperation.m:292
      frame #20: 0x001f4f8a APP`-[SyncRequestOperation main] + 890 at SyncRequestOperation.m:385
      frame #21: 0x01c61d23 Foundation`-[__NSOperationInternal start] + 736
      frame #22: 0x01c61a34 Foundation`-[NSOperation start] + 79
      frame #23: 0x01cee301 Foundation`__block_global_6 + 150
      frame #24: 0x03d8853f libdispatch.dylib`_dispatch_call_block_and_release + 15
      frame #25: 0x03d9a014 libdispatch.dylib`_dispatch_client_callout + 14
      frame #26: 0x03d8b2e8 libdispatch.dylib`_dispatch_root_queue_drain + 335
      frame #27: 0x03d8afcb libdispatch.dylib`_dispatch_worker_thread3 + 20
      frame #28: 0x9a1f0b24 libsystem_c.dylib`_pthread_wqthread + 346
  </code></pre>