skip to Main Content

At some point during the development of my SwiftUI app the canvas preview stopped loading. The app uses CoreData, and the crash log suggests issues with the persistence function. The app continues to work as expected on the device and using the simulator.

Top part of the crash log from preview:

Path:                  /Users/USER/Library/Developer/Xcode/UserData/Previews/Simulator Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Bundle/Application/0011D911-75A5-4D79-AFA2-34BA2CB9E805/Training.app/Training
Identifier:            Training
Version:               1.5 (11)
Code Type:             X86-64 (Native)
Parent Process:        launchd_sim [80779]
Responsible:           SimulatorTrampoline [984]
User ID:               501

Date/Time:             2021-08-17 16:55:41.451 +0100
OS Version:            macOS 11.5 (20G71)
Report Version:        12
Bridge OS Version:     3.0 (14Y908)
Anonymous UUID:        A9FE029F-99BA-7511-2780-E515C2BA21E9

Sleep/Wake UUID:       EA1649DE-D2A1-438F-8B86-9C8D63032F7F

Time Awake Since Boot: 160000 seconds
Time Since Wake:       11000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes:       0x0000000000000001, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Illegal instruction: 4
Termination Reason:    Namespace SIGNAL, Code 0x4
Terminating Process:   exc handler [81046]

Application Specific Information:
Training/Persistence.swift:17: Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={sourceURL=file:///Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator%20Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application%20Support/Training.sqlite, reason=Cannot migrate store in-place: I/O error for database at /Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application Support/Training.sqlite.  SQLite error code:1, 'no such table: ZPROGRAMS', destinationURL=file:///Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator%20Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application%20Support/Training.sqlite, NSUnderlyingError=0x6000016b1200 {Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={NSSQLiteErrorDomain=1, NSFilePath=/Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application Support/Training.sqlite, NSUnderlyingException=I/O error for database at /Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application Support/Training.sqlite.  SQLite error code:1, 'no such table: ZPROGRAMS', reason=I/O error for database at /Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application Support/Training.sqlite.  SQLite error code:1, 'no such table: ZPROGRAMS'}}}, ["reason": Cannot migrate store in-place: I/O error for database at /Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application Support/Training.sqlite.  SQLite error code:1, 'no such table: ZPROGRAMS', "NSUnderlyingError": Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={NSSQLiteErrorDomain=1, NSFilePath=/Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application Support/Training.sqlite, NSUnderlyingException=I/O error for database at /Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application Support/Training.sqlite.  SQLite error code:1, 'no such table: ZPROGRAMS', reason=I/O error for database at /Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application Support/Training.sqlite.  SQLite error code:1, 'no such table: ZPROGRAMS'}, "sourceURL": file:///Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator%20Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application%20Support/Training.sqlite, "destinationURL": file:///Users/liamday/Library/Developer/Xcode/UserData/Previews/Simulator%20Devices/836D94C3-1BF9-40BD-88EA-85DDF55F71E0/data/Containers/Data/Application/723C3942-DBEE-4A0E-BBBE-5F3B7F5F01A1/Library/Application%20Support/Training.sqlite]
 
CoreSimulator 757.5 - Device: iPhone 12 Pro (836D94C3-1BF9-40BD-88EA-85DDF55F71E0) - Runtime: iOS 14.5 (18E182) - DeviceType: iPhone 12 Pro

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libswiftCore.dylib              0x00007fff2f07cd1e _assertionFailure(_:_:file:line:flags:) + 1742
1   co.uk.LiamDay.Training          0x0000000105d3266a closure #1 in PersistenceController.init(inMemory:) + 890 (Persistence.swift:17)
2   co.uk.LiamDay.Training          0x0000000105d326fb thunk for @escaping @callee_guaranteed (@guaranteed NSPersistentStoreDescription, @guaranteed Error?) -> () + 139
3   CoreData                        0x00007fff251a7838 -[NSPersistentStoreCoordinator _doAddPersistentStoreWithDescription:privateCopy:completeOnMainThread:withHandler:] + 978
4   CoreData                        0x00007fff251a7c3a -[NSPersistentStoreCoordinator addPersistentStoreWithDescription:completionHandler:] + 236
5   CoreData                        0x00007fff2514b01b -[NSPersistentContainer _loadStoreDescriptions:withCompletionHandler:] + 272
6   CoreData                        0x00007fff2514aee4 -[NSPersistentContainer loadPersistentStoresWithCompletionHandler:] + 398
7   co.uk.LiamDay.Training          0x0000000105d32144 PersistenceController.init(inMemory:) + 1060 (Persistence.swift:15)
8   co.uk.LiamDay.Training          0x0000000105d31d01 one-time initialization function for shared + 17 (Persistence.swift:6)
9   libdispatch.dylib               0x00007fff201078df _dispatch_client_callout + 8
10  libdispatch.dylib               0x00007fff20108b16 _dispatch_once_callout + 20
11  libswiftCore.dylib              0x00007fff2f3a059a swift_once + 26
12  co.uk.LiamDay.Training          0x0000000105d32234 PersistenceController.shared.unsafeMutableAddressor + 52 (Persistence.swift:6)
13  co.uk.LiamDay.Training          0x0000000105e73ba0 TrainingApp.init() + 80 (TrainingApp.swift:12)
14  co.uk.LiamDay.Training          0x0000000105e73ce1 protocol witness for App.init() in conformance TrainingApp + 17
15  com.apple.SwiftUI               0x00007fff562e5376 static App.main() + 47
16  co.uk.LiamDay.Training          0x0000000105e73c6e static TrainingApp.$main() + 78 (TrainingApp.swift:10)
17  co.uk.LiamDay.Training          0x0000000105e73d04 main + 20
18  libdyld.dylib                   0x00007fff2025abbd start + 1

Thread 1:
0   libsystem_pthread.dylib         0x00007fff6033f420 start_wqthread + 0

Thread 2:
0   libsystem_pthread.dylib         0x00007fff6033f420 start_wqthread + 0

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000200000003  rbx: 0x0000000106005a90  rcx: 0xfffffffe00000000  rdx: 0x0000000000000003
  rdi: 0x00007fe683033208  rsi: 0x0000000000000103  rbp: 0x00007ffee9ed0430  rsp: 0x00007ffee9ed0390
   r8: 0x0000000106868a00   r9: 0x00000001068603e0  r10: 0x0000000000000040  r11: 0x000000000000003b
  r12: 0x0000000000000f30  r13: 0x00007ffee9ed0350  r14: 0x000000000000000b  r15: 0x00007ffee9ed0390
  rip: 0x00007fff2f07cd1e  rfl: 0x0000000000010246  cr2: 0x00007fff2067e4ee
  
Logical CPU:     1
Error Code:      0x00000000
Trap Number:     6

Thread 0 instruction stream:
  fe fe fe fe fe fe 00 b9-20 00 00 00 45 85 c0 74  ........ ...E..t
  07 41 0f bd c8 83 f1 1f-c1 e9 03 be 04 00 00 00  .A..............
  48 29 ce 8d 0c f5 00 00-00 00 48 c7 c2 ff ff ff  H)........H.....
  ff 48 d3 e2 48 f7 d2 44-89 c1 48 01 c1 48 21 d1  .H..H..D..H..H!.
  48 89 4d b8 48 8d 15 77-15 00 00 48 8d 7d b8 e8  H.M.H..w...H.}..
  1e 35 2d 00 4c 89 fc 48-8b 7d d0 e8 12 df 32 00  .5-.L..H.}....2.
 [0f]0b 55 48 89 e5 31 c0-5d c3 0f 1f 84 00 00 00  ..UH..1.]....... <==
  00 00 55 48 89 e5 41 57-41 56 41 55 41 54 53 50  ..UH..AWAVAUATSP
  4d 89 cc 45 89 c6 49 89-cf 48 89 d3 49 89 f5 ff  M..E..I..H..I...
  d7 49 89 d0 48 8d 3d bb-14 38 00 be 0b 00 00 00  .I..H.=..8......
  ba 02 00 00 00 48 89 c1-49 89 d9 6a 00 41 54 41  .....H..I..j.ATA
  56 41 57 e8 da f8 ff ff-48 83 c4 20 0f 0b 0f 1f  VAW.....H.. ....
  
Thread 0 last branch register state not available.

This is my Persistence.swift file:

import CloudKit

struct PersistenceController {
    //MARK: - 1. PERSISTENCE CONTROLLER
    static let shared = PersistenceController()
    //MARK: - 2. PERSISTENT CONTAINER
    let container: NSPersistentContainer
    //MARK: - 3. INIT (load the persistent store)
    init(inMemory: Bool = false) {
        container = NSPersistentContainer(name: "Training")
        if inMemory {
            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
        }
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error (error), (error.userInfo)")
            }
        })
    }
    //MARK: - 4. PREVIEW
    static var preview: PersistenceController = {
        let result = PersistenceController(inMemory: true)
        let viewContext = result.container.viewContext
        
        let plannedExercise1 =  PlannedWorkouts(context: viewContext)
        plannedExercise1.dateCreated = Date()
        plannedExercise1.dateOfWorkout = Date()
        plannedExercise1.duration = 3600.00
        plannedExercise1.totalDistanceUnit = "meters"
        plannedExercise1.totalDistanceDoubleValue = 5000.0
        plannedExercise1.totalEnergyBurnedUnit = "KCal"
        plannedExercise1.totalEnergyBurnedDoubleValue = 500.0
        plannedExercise1.workoutActivityType = 32
        plannedExercise1.id = UUID()
        
        let plannedExercise2 =  PlannedWorkouts(context: viewContext)
        plannedExercise2.dateCreated = K.init().EndOfWeek(weeksSinceAnchorDate: 1)
        plannedExercise2.dateOfWorkout = Date()
        plannedExercise2.duration = 3600.00
        plannedExercise2.totalDistanceUnit = "meters"
        plannedExercise2.totalDistanceDoubleValue = 5000.0
        plannedExercise2.totalEnergyBurnedUnit = "KCal"
        plannedExercise2.totalEnergyBurnedDoubleValue = 500.0
        plannedExercise2.workoutActivityType = 35
        plannedExercise2.id = UUID()
        
        let newStatisticChartView = ChartView(context: viewContext)
        newStatisticChartView.id = UUID()
        newStatisticChartView.chartViewRawValue = "dailyTrainingImpactChartView"
        
        let newGoalView = GoalViews(context: viewContext)
        newGoalView.startDate = K.init().EndOfWeek(weeksSinceAnchorDate: 1)
        newGoalView.endDate = K.init().EndOfWeek(weeksSinceAnchorDate: 2)
        newGoalView.id = UUID()
        newGoalView.totalDistanceUnit = "meters"
        newGoalView.totalDistanceDoubleValue = 30000
        newGoalView.workoutActivityType = Int16(32)
        
        let newProgram = Programs(context: viewContext)
        
        let newProgrammedWorkout = ProgrammedWorkouts(context: viewContext)
        
        let newPlannedProgram = PlannedPrograms(context: viewContext)
        
        
        do {
            try viewContext.save()
        } catch {
            let nsError = error as NSError
            fatalError("Unresolved error (nsError), (nsError.userInfo)")
        }
        return result
    }()
}

I have restarted Xcode and cleared the derived data. What should I try next? Or is this the limitations of the previewer with SwiftUI for complex app builds?

2

Answers


  1. The answer to your question is in the long string of the debug output. Simply put, there is a problem with updating the database Training.sqlite which is created automatically by the persistent container framework.

    Try deleting these folders / files manually. You can find the full path in the error message.

    Also, as pointed out by other posters, it is advisable to use PersistentController.preview when constructing the preview database. It will then always be created from scratch in memory.

    Login or Signup to reply.
  2. Are you using PersistenceController.preview or PersistenceController.shared in your preview? Always use .preview in your previews, because it avoids saving the database to disk — based on your error, it looks like it’s trying to save to disk, which will sort of work but you’ll run into errors. Also be sure your views aren’t calling PersistenceController.shared and instead pass the context in or use an environment variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search