func with completion



I’m unit testing some functions with completion handlers but I’m unable to get the callbacks to fire   Here is the code being tested:

class MyClass1 {
    static func updateName(with myStruct: inout, completion: @escaping (Error?) -> Void) {
        var newName: String?
        var updateError: Error?
        MyClass2.createNewName( {
            (name, error) in
            newName = name
            updateError = error
        if let newName = newName {
   = newName
class MyClass2 {
    static func createNewName(_ oldName: String, @escaping (String?, Error?) -> Void) {
        let task = URLSession.shared.dataTask(with: URL(string: "https:/
            (data, response, error) in
            guard error == nil else {
                return completion(nil, error!)
            let result = String(data: data!, .utf8)
            completion(result, error)
struct MyStruct {
    private var _name: String
    var Name: String {
        get {
            return _name
        set {
            _name = newValue



When I execute MyClass2.createNewName directly, the completion block executes and the function suceeds fine.


However, when I call MyClass1.updateName which calls into MyClass2.createNewName the completion block in MyClass2.createNewName doesn’t get executed and the = newName is never hit.


What could I be doing wrong?  And any suggestions to resolve this



Powered by WPeMatico


You may also like...

Comments are closed.