无法使用CoreStore添加记录

时间:2018-10-21 04:43:37

标签: swift corestore

我正在尝试将记录保存在CoreStore中,但是似乎没有任何作用。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    CoreStore.defaultStack = DataStack(
        CoreStoreSchema(
            modelVersion: "V1",
            entities: [
                Entity<WorkoutModel>("Workout", isAbstract: false)
            ]
        )
    )
    CoreStore.addStorage(
        SQLiteStore(fileName: "MyStore.sqlite"),
        completion: { (result) -> Void in
            print(CoreStore.defaultStack.modelSchema.printCoreStoreSchema())
            CoreStore.perform(
                asynchronous: { (transaction) -> Void in
                    let person = transaction.create(Into<WorkoutModel>())
                    person.routine.value = "test routine"
            },
                completion: { (result) -> Void in
                    switch result {
                    case .success: self.loadRoutine()
                    case .failure(let error): print(error)
                    }
            }
            )})
    return true
}

func loadRoutine() -> Void {
    print("successfully saved.")
    var place = CoreStore.fetchAll(From<WorkoutModel>())
    if place == nil {

        _ = try? CoreStore.perform(
            synchronous: { (transaction) in

                let place = transaction.create(Into<WorkoutModel>())
                place.routine.value = "Another routine"
        }
        )
        place = CoreStore.fetchAll(From<WorkoutModel>())
    }
}

我忘了包括我的模特:

class WorkoutModel: CoreStoreObject {
    var routine = Value.Required<String>("routine", initial: "")
}

它将打印出架构,我知道我进入了loadRoutine函数,但是似乎什么也没发生。

0 个答案:

没有答案