Swift: Convert a Sync function to Async - swift

If you have a sync function, how would you convert it to an async function?
func syncFunc() -> Int {
//Do something
}
Would this work?
func asyncFunc(_ syncFunc:()->Int, _ completion:(Int)->()) -> Int {
DispatchQueue.background.async{
completion( syncFunc() )
}
}

No, functions containing an asynchronous task cannot return any value from the closure body and both closures must be marked as #escaping
func asyncFunc(_ syncFunc: #escaping ()->Int, completion: #escaping (Int)->()) {
DispatchQueue.global().async {
completion( syncFunc() )
}
}

Related

Invalid conversion from throwing function of type XXXX to non-throwing function type XXXX

I am stuck with this situation where I have a custom JSONDecoder struct which contains a private function to decode data, and another function which is exposed, and should return a specific, Decodable type. I would like these functions to throw successively so I only have to write my do/catch block inside the calling component, but I'm stuck with this error on the exposedFunc() function:
Invalid conversion from throwing function of type '(Completion) throws -> ()' (aka '(Result<Data, any Error>) throws -> ()') to non-throwing function type '(Completion) -> ()' (aka '(Result<Data, any Error>) -> ()')
Here is the code:
import Foundation
import UIKit
typealias Completion = Result<Data, Error>
let apiProvider = ApiProvider()
struct DecodableTest: Decodable {
}
struct CustomJSONDecoder {
private static func decodingFunc<T: Decodable>(
_ response: Completion,
_ completion: #escaping (T) -> Void
) throws {
switch response {
case .success(let success):
try completion(
JSONDecoder().decode(
T.self,
from: success
)
)
case .failure(let error):
throw error
}
}
static func exposedFunc(
value: String,
_ completion: #escaping (DecodableTest) -> Void
) throws {
apiProvider.request {
try decodingFunc($0, completion)
}
}
}
class CustomViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
do {
try CustomJSONDecoder.exposedFunc(value: "test_value") { result in
// Do something with result
}
} catch {
print(error)
}
}
}
class ApiProvider: NSObject {
func request(_ completion: #escaping (Completion) -> ()) {
}
}
Thank you for your help
This defines method that takes a non-throwing function:
class ApiProvider: NSObject {
func request(_ completion: #escaping (Completion) -> ()) {
}
}
So in all cases, this function must take a Completion and return Void without throwing. However, you pass the following:
apiProvider.request {
try decodingFunc($0, completion)
}
This method does throw (note the uncaught try), so that's not allowed. You need to do something if this fails:
apiProvider.request {
do {
try decodingFunc($0, completion)
} catch {
// Here you must deal with the error without throwing.
}
}
}
Not using concurrency features is making your code hard to understand. Switch!
final class CustomViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Task {
let result = DecodableTest()
// Do something with result
}
}
}
extension DecodableTest {
init() async throws {
self = try JSONDecoder().decode(Self.self, from: await APIProvider.data)
}
}
enum APIProvider {
static var data: Data {
get async throws { .init() }
}
}

Escaping closure captures non-escaping parameter 'promise'

i am trying to use a publisher, but i got this error: "Escaping closure captures non-escaping parameter 'promise'" in the Timer. line.
How can i solve this?
extension OperationQueue {
func publisher<Output, Failure: Error>(_ block: #escaping (Future<Output, Failure>.Promise) -> Void) -> AnyPublisher<Output, Failure> {
Future<Output, Failure> { promise in
self.addOperation {
block(promise)
}
}.eraseToAnyPublisher()
}
}
struct ContentView: View {
func getDataViaPublisher()->AnyPublisher<String,Error> {
OperationQueue.main.publisher { promise in
Timer.scheduledTimer(withTimeInterval: Double.random(in: 1...3), repeats: false) { (timer) in. // <<<<< error here
promise(.success("Chris"))
}
}
}
Promise is also closure, so you need to make it #escaping in arguments as well.
Here is fixed extension
extension OperationQueue {
func publisher<Output, Failure: Error>(_ block: #escaping (#escaping Future<Output, Failure>.Promise) -> Void) -> AnyPublisher<Output, Failure> {
Future<Output, Failure> { promise in
self.addOperation {
block(promise)
}
}.eraseToAnyPublisher()
}
}

How to chain functions with a completion handler in Swift?

I am chaining some functions together and I can't figure out how to call a completion handler with a return value once all the functions are done running.
func getAirQuality(completion: (aqi: Int?) -> Void) {
callAPI()
}
private func callAPI() {
// ... get data
self.parseDataForAQI(data: data)
}
private func parseDataForAQI(data: Data) {
let aqi = aqi
// Send aqi up to completion handler in getAirQuality
}
So that when everything is said and done I can just do something like this:
getAirQuality(completion: { aqi -> Void in {
// Do something with aqi
})
My first assumption is that your first 3 functions are part of a class. If so, one approach is to save the completion handler as an instance variable.
class AirQualityProvider {
var aBlock: ((Int?) -> Void)?
func getAirQuality(completion: #escaping (Int?) -> Void) {
aBlock = completion
callAPI()
}
private func callAPI() {
let data = Data()
parseDataForAQI(data: data)
}
private func parseDataForAQI(data: Data) {
let aqi = 1
if let completion = aBlock {
completion(aqi)
}
}
}
Here's an example of a caller as written in a playground.
let aqp = AirQualityProvider()
aqp.getAirQuality { (value) in
if let value = value {
print("Value = \(value)")
}
}

Use function in closure

I am trying to use a function within a closure but I am receiving an error 'cannot convert value of type () to closure result type Bool'. The following code demonstrates the error. How can I make this work?
func test1(){
test2(){ success in
self.test1()
}
}
func test2(completionHandler: (Bool) -> Bool){
completionHandler(true)
}
You specify that the test2 closure returns a Bool, so return one:
func test1(){
test2 { (success) -> Bool in
test1()
return success
}
}
Have test2's closure return void if you don't want to return a value from it:
func test1(){
test2 { (success) in
test1()
}
}
func test2(completionHandler: (Bool) -> Void){
completionHandler(true)
}

Async Download of Information from server

I want to download data from my server to be displayed on a map. Therefore I use async methods to get the data. The goal is to have an array of annotation objects to be displayed.
Therefore I first download Information A and then Information B. As both are async methods, I guess I need to wait for the completionHandler to return true so I know the data is loaded. This is easy for one method. But how do I handle to wait for both methods before the completionHandler of getInformationFromServer returns true and triggers therefore the addition of annotations?
override func viewWillAppear(animated: Bool) {
self.customizeInterface()
self.getInformationFromServer { (completed) -> Void in
if(completed) {
self.mapView.addAnnotations(self.annotationArray)
}
}
}
func getInformationFromServer(completionHandler: (completed: Bool) -> Void) {
getInformationFromServerA { (downloadCompleted) -> Void in
completionHandler(completed: downloadCompleted)
}
// HOW DO I MANAGE TO ONLY RETURN THE COMPLETION HANDLER TRUE WHEN
// BOTH FUNCTIONS RETURNED TRUE?
}
func getInformationFromServerA(completionHandler: (downloadCompleted: Bool) -> Void) {
Server().getJsonInformationFromServer(url: "aeds", completionHandler: { (response) -> Void in
self.parseAEDInformationToAnnotation(response["data"])
completionHandler(downloadCompleted: true)
})
}
func getInformationFromServerB(completionHandler: (downloadCompleted: Bool) -> Void) {
Server().getJsonInformationFromServer(url: "aeds", completionHandler: { (response) -> Void in
self.parseAEDInformationToAnnotation(response["data"])
completionHandler(downloadCompleted: true)
})
}
You may use a dispatch group to wait until both downloads finish.
func getInformationFromServer(completionHandler: (completed: Bool) -> Void) {
let dispatchGroup = dispatch_group_create()
var downloadCompletedA: Bool = false
dispatch_group_enter(dispatchGroup)
getInformationFromServerA { (downloadCompleted) -> Void in
downloadCompletedA = downloadCompleted
dispatch_group_leave(dispatchGroup)
}
var downloadCompletedB: Bool = false
dispatch_group_enter(dispatchGroup)
getInformationFromServerB { (downloadCompleted) -> Void in
downloadCompletedB = downloadCompleted
dispatch_group_leave(dispatchGroup)
}
// wait until both downloads are finished
dispatch_group_wait(dispatchGroup, DISPATCH_TIME_FOREVER)
completionHandler(downloadCompletedA && downloadCompletedB)
}
See Apple's Concurrency Programming Guide:
Dispatch groups are a way to block a thread until one or more tasks
finish executing. You can use this behavior in places where you cannot
make progress until all of the specified tasks are complete.
Another solution which I can recommend for you and it's not so "complex" as solution with dispatch_group_wait:
func getInformationFromServer(completionHandler: (completed: Bool) -> Void) {
getInformationFromServerA { [weak self] (downloadCompleted: Bool) -> Void in
if downloadCompleted {
self?.getInformationFromServerB({ (downloadCompleted: Bool) -> Void in
completionHandler(completed: downloadCompleted)
})
}
else {
completionHandler(completed: downloadCompleted)
}
}
}