我使用以下代码创建对象:
CoreStore.perform(
asynchronous: { (transaction) -> Void in
let summary = transaction.create(Into<SummaryEntity>())
},
completion: { _ in }
)
最后,我想返回刚刚创建的摘要对象。
该怎么做?
我做了类似的事情,但是不确定我们需要大量的拆包和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
展开的错误:
答案 0 :(得分:1)
由于您的示例完全不同,我不确定您要问什么。
如果要创建对象,将执行与导入相同的操作。 transaction.create不返回可选值,因此只需在第一个闭合中返回对象:
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
// ...
})