robertsan
robertsan

Reputation: 1621

Swift Delegate from Singleton not working

I'm trying to implement SharedInstanceDelegate in App class. I have no idea why the functions under the protocol are not being called.

This is my Protocol and class.

class App {

    let sharedInstance = SharedInstance.shared

    init() {
        self.sharedInstance.delegate = self
    }
}

extension App: SharedInstanceDelegate {

    func1() { } // this is not executed
    func2() { }
}

protocol SharedInstanceDelegate: class {

   func1()
   func2()
}

class SharedInstance {

    static let shared = SharedInstance()
    weak var delegate: SharedInstanceDelegate?

    private init() { }

    func method1() {

        self.delegate?.func1() // this is executed
    }
}

Upvotes: 1

Views: 1422

Answers (4)

JohnO
JohnO

Reputation: 41

Your code could work but it depends on how you are calling func1(). Calling it like this:

let testinstance = App().sharedInstance
testinstance.delegate?.func1()

will not work because you are not holding on to the App object. In this case the App object is the delegate, but because its a weak member and no one is retaining it, it gets released right away.

If you call it like this:

let testapp = App()
testapp.sharedInstance.delegate?.func1()

it works. In this case the App object is being retained and is still around when func1() is called.

Either way the way these classes are related is confusing to me. Why have a separate SharedInstance class at all?

Upvotes: 0

robertsan
robertsan

Reputation: 1621

Still no idea why this was happening, but cleaning the project fixed this. This is very strange. I have other delegates that call successfully.

Upvotes: 0

Prientus
Prientus

Reputation: 733

Here is how I would implement your code to work with the delegate:

class App {

    let sharedInstance = SharedInstance.shared

    init() {
        self.sharedInstance.delegate = self
    }
}

extension App: SharedInstanceDelegate {

    func func1() { } // this will run now
    func func2() { }
}

protocol SharedInstanceDelegate {
    func func1()
    func func2()
}

class SharedInstance {

    static let shared = SharedInstance()
    var delegate: SharedInstanceDelegate?

    private init() { }

    func method1() {

        self.delegate?.func1() // this is executed
    }
}

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299613

I believe you meant to make SharedInstanceDelegate a protocol, but you've made it a class. In either case, App does not conform/inherit SharedInstanceDelegate, so it's not clear how this would even compile.

Upvotes: 4

Related Questions