Search code examples
corestore

CoreStore Xcode data fault issue when fetching objects


I'm new to CoreStore and attempting to test my ability to create, store, and fetch CoreStore Objects correctly. It appears entries are being created based on results of dataStack.fetchCount() and transaction.create(), but the data is unavailable or not stored correctly based on my results of dataStack.fetchAll() (it says data: fault at the end of each object) and inspection of the SQLite database with Liya software reveals no entries in the table. I'm using CoreStore v8.0.1 and Xcode v12.4. My example code (using synchronous execution) is below. I appreciate your input into troubleshooting the issue. Thanks.

MyImage.swift:

import CoreStore
final class MyImage: CoreStoreObject {
    @Field.Stored("desc")
    var desc: String = ""
}

MyApp.swift

import SwiftUI
import CoreStore
import Foundation

@main
struct MyApp: App {
    
    static let dataStack: DataStack = {
        let dataStack = DataStack(
            CoreStoreSchema(
                modelVersion: "V1",
                entities: [
                    Entity<MyImage>("MyImage")
                ],
                versionLock: [
                    "MyImage": [..., ..., ..., ...]
                ]
            )
        )
        
        try! dataStack.addStorageAndWait(
            SQLiteStore( fileName: "MyStore.sqlite",
                //localStorageOptions: .allowSynchronousLightweightMigration,
                localStorageOptions: .recreateStoreOnModelMismatch
            )
        )
        print(dataStack.coreStoreDumpString)
        return dataStack
    }()
    
    init() {
        CoreStoreDefaults.dataStack = MyApp.dataStack
        try! dataStack.perform(
            synchronous: { (transaction) in
                let test_obj = transaction.create(Into<MyImage>())
                test_obj.desc = "Description"
            },
            waitForAllObservers: true
        )
        
        do {
            let objects = try dataStack.fetchAll(From<MyImage>())
            print(objects)
        } catch { print("error fetching")}
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

I appreciate your help.


Solution

  • It turns out that the fetch results will display data:fault until the properties of the objects are accessed. The following example code will display the value of the properties:

    let objects = try dataStack.fetchAll(From<MyImage>())
    print(objects.map { $0.desc })