0

I am using Siesta framework and trying to add decorator in order to refresh token when it expires but I am getting: 'self captured by a closure before all members were initialized'.

What could be the reason?

service.configure("**") {
      $0.decorateRequests {
      self.refreshTokenOnAuthFailure(request: $1) // this line complains
    }
}

UPDATE

I found my problem and wanted to share it with you. The problem is related to services which were class properties:

class API: NSObject {

 private let service = Service(
    baseURL: myApiBaseUrl,
    standardTransformers: [.text, .json]
)

override init() {
    #if DEBUG
        // Bare-bones logging of which network calls Siesta makes:
        LogCategory.enabled = [.network]
    #endif

    service.configure("**") {

       $0.headers["Token"] = "Bearer \(token)"
       $0.headers["Content-Type"] = "application/json"
       $0.headers["Accept"] = "application/json"

       $0.decorateRequests {
        self.refreshTokenOnAuthFailure(request: $1)
       }
    }

}

Instead of using a class property, I moved my service outside of the class and added a designated initializer.

init(myService:Service){
    super.init()

    myService.configure("**") {

        $0.headers["Token"] = "Bearer \(token)"
        $0.headers["Content-Type"] = "application/json"
        $0.headers["Accept"] = "application/json"

        $0.decorateRequests {
            self.refreshTokenOnAuthFailure(request: $1)
        }
    }

}
Ugur
  • 3
  • 3

2 Answers2

1

You might wanna add [unowned self] at the beginning of the closure that way the the closure is not retained. Please try [weak self ] also

Kunal_D
  • 29
  • 4
  • Thank you for your contribution. Could you perhaps include this in OPs code, as to show how it is applied. – RandomSort May 16 '18 at 12:51
  • 2
    `service.configure("**") {[self unowned] $0.decorateRequests { self.refreshTokenOnAuthFailure(request: $1) // this line complains } }` – Kunal_D May 16 '18 at 12:52
  • Awesome, if you now edit your answer, I'd love to upvote it :) – RandomSort May 16 '18 at 12:54
  • Kunal when I add [self unowned] it is giving use of unresolved identifier for unowned – Ugur May 16 '18 at 13:12
  • I tried this and it gives exactly the same error I got: $0.decorateRequests {[unowned self] rep, req in self.refreshTokenOnAuthFailure(request: req) } – Ugur May 16 '18 at 13:56
  • Thanks Kunal what you suggested worked after I create a custom init but I now have a different Siesta related issue. – Ugur May 16 '18 at 14:15
  • @RandomSort I understood my mistake and I corrected it , thanks. Nice to meet you – Kunal_D May 16 '18 at 18:44
0

The error message is telling you what the problem is:

error: 'self' captured by a closure before all members were initialized

You are trying to capture self before all members were initialized. Consider the following two examples, one shows the error you are having and the other doesn't.

Example of your error

class Customer {

    var someProperty: String
    var someArray: [Int] = [1,2,3]

    init() {

        someArray.forEach {
            print("\($0): \(self.someProperty)") // Error: 'self' captured before initializing 'someProperty'
        }

        someProperty = "Potato"
    }
}

_ = Customer()

Solution

class Customer {

    var someProperty: String
    var someArray: [Int] = [1,2,3]

    init() {

        someProperty = "Potato"

        someArray.forEach {
            print("\($0): \(self.someProperty)") // Good, 'someProperty' has been initialized already
        }
    }
}

_ = Customer()


// Output:
//
// 1: Potato
// 2: Potato
// 3: Potato
R.B.
  • 422
  • 4
  • 10