I use this code to create an object:
CoreStore.perform(
asynchronous: { (transaction) -> Void in
let summary = transaction.create(Into<SummaryEntity>())
},
completion: { _ in }
)
In completion I would like to return back just created summary object.
How to do this?
I did something like that, but not sure for what we need a lot of unwraps and fetchExisting
function
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
// ...
}
)
This is an error of unwrapping from fetchExisting
:
I am not exactly sure what you are asking as your examples are completely different.
If you want to create an object, you would do the same as for importing. transaction.create does not return an optional, so just have your closure in the first one return the object:
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
// ...
})