TezosKit icon indicating copy to clipboard operation
TezosKit copied to clipboard

weak self in NetworkClient never returns results

Open utdrmac opened this issue 5 years ago • 0 comments

I'm not a Swift coder. Just started learning a couple days ago and I've jumped right into the deep end.

I tried using this example code from your project:

        Logger.shared.logLevel = .debug
        let publicNodeURL = URL(string: "https://delphinet-tezos.giganode.io")
        let tk = TezosNodeClient(remoteNodeURL: publicNodeURL!, tezosProtocol: .delphi)
        
        tk.getHead() { result in
            switch result {
            case .success(let result):
              guard let metadata = result["metadata"] as? [String : Any],
                    let baker = metadata["baker"]  else {
                print("Unexpected format")
                return
              }
              print("Baker of the block at the head of the chain is \(baker)")
            case .failure(let error):
              print("Error getting result: \(error)")
            }
        }

I had to edit some files to add .delphi enum, but that's all I change initially. Running the above code, I can see messages in console:

>>>>>> Request
Endpoint: https://delphinet-tezos.giganode.io/chains/main/blocks/head
Headers: 
>>>>>> End Request

But that's it. I never get the response. I added some logging to NetworkClient.swift:

        guard let self = self else {
            Logger.shared.log(">>>>> Self is not self!", level: .debug)
            return
        }

And re-run, and now I see this:

>>>>>> Request
Endpoint: https://delphinet-tezos.giganode.io/chains/main/blocks/head
Headers: 
>>>>>> End Request
>>>>> Self is not self!

If I remove the [weak self], then everything seems to work as intended. Is this something that's not needed in Swift 5? I understand this has to do with references for memory leaks. I certainly don't want that, but I also want to get this library working :)

utdrmac avatar Dec 15 '20 04:12 utdrmac