CoreStore 创建对象并 return 成功

CoreStore creating object and return it in success

我使用这段代码创建了一个对象:

CoreStore.perform(
            asynchronous: { (transaction) -> Void in
                let summary = transaction.create(Into<SummaryEntity>())

        },
            completion: { _ in }
        )

完成后我想return返回刚刚创建的摘要对象。

如何操作?

我做了类似的事情,但不确定我们需要大量解包和 fetchExisting 功能

CoreStore.perform(
            asynchronous: { (transaction) -> Routine? in

                let routine = try! transaction.importUniqueObject(
                    Into<Routine>(),
                    source: routineDictionary)

                return routine
        },
            success: { (transactionRoutine) in

                guard let unwrappedTransactionRoutine = transactionRoutine else {
                    return
                }

                let routine = CoreStore.fetchExisting(unwrappedTransactionRoutine)

                guard let unwrappedRoutine = routine else {
                    return
                }

                completion(.data(unwrappedRoutine))
        },
            failure: { (error) in
                // ...
        }
        )

这是从 fetchExisting 展开的错误:

我不太确定你在问什么,因为你的例子完全不同。

如果你想创建一个对象,你会做同样的导入。 transaction.create 不是 return 可选的,所以只需在第一个 return 对象中包含闭包:

CoreStore.perform(asynchronous: { (transaction) -> SummaryEntity in
    let summary = transaction.create(Into<SummaryEntity>())
    return summary
}, success: { (summary) in
    let fetchedSummary = CoreStore.fetchExisting(summary)
    completion(.data(fetchedSummary))
}, failure: { (error) in
    // ...
})