0

As you can see, I have a very complicated function:

func save(records: [CKRecord], recordIDsToDelete: [CKRecord.ID], handler: CountHandler?, completion: RequestsHandler?) {}

That function I will rewrite to async-await in the future. But for now, I need to inject it into the current code. How can I do it to make it work?

private func save(records: [CKRecord], handler: ProgressHandler?) throws -> Bool {
    save(records: records, recordIDsToDelete: []) { count in
        handler?("Saving\n\(count)\nrecords.")
    } completion: { _, _, _, error in
        guard let error = error else {
            return true
            return
        }
        throw error
    }
}

What do I need to have? I need to know if there was a success or failure, and if failure, I need to know the error.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 1
    *I will rewrite to async-await also*. **Do it now**! The API to save (and also to delete) records `modifyRecords(saving:deleting:savePolicy:atomically:)` is `async`. You can't return a Bool from a completion closure synchronously. – vadian Feb 16 '23 at 17:27
  • @vadian you are completely right. There is no better time to rewrite than now;) – Bartłomiej Semańczyk Feb 16 '23 at 17:29
  • [Here](https://github.com/apple/sample-cloudkit-privatedb) are samples by Apple how to use CloudKit with async/await. – vadian Feb 16 '23 at 17:32
  • I can't post an answer, but - you can wrap into async-await only the `completion` part. 1: `try await withCheckedThrowingContinuation { cont in`. 2: call your function. 3: in the `completion` handler, instead of writing `return`, you write `cont.resume`. Here's the doc: https://developer.apple.com/documentation/swift/checkedcontinuation – Isaaс Weisberg Feb 17 '23 at 07:27
  • @vadian, after some time *Do it now! * was one of the best advices I received in the past few years. `Async await throws` is phenomenal;) Thank you – Bartłomiej Semańczyk Mar 09 '23 at 21:50

0 Answers0