Skip to content

Retain cycle in bind(to:input:) #227

@nflahavan

Description

@nflahavan

Like the title says there is a retain cycle in the various bind(to:input:) functions. The following test fails:

it("does not create a retain cycle") {
  var subject: UIBarButtonItem? = UIBarButtonItem(barButtonSystemItem: .save, target: nil, action: nil)
  let action = Action<String, String>(workFactory: { _ in
      return .empty()
  })

  subject?.rx.bind(to: action, input: "Hi there!")

  weak var subjectWeakReference = subject
  subject = nil

  expect(subjectWeakReference).to(beNil()) // fails!
}

The retain cycle is due to passing base to inputTransform in map. This can be fixed by changing the various bind methods to the following:

self.tap // or controlEvent
    .withUnretained(self.base) // no longer retaining base!
    .map(\.0)
    .map(inputTransform)
    .bind(to: action.inputs)
    .disposed(by: self.base.actionDisposeBag)

With the change above all tests plus the one I added to check for a retain cycle pass. I'd be happy to write up a pull request to make these changes if you agree they need to be made.

P.s. Thanks for the great framework!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions