45

Do i need to use [weak self] within RXSwift subscribeNext closures?

I have the code:

    searchController.searchBar.rx_text.throttle(0.2, scheduler: MainScheduler.instance).subscribeNext { searchText in
        self.viewModel.searchForLocation(searchText)
    }.addDisposableTo(DisposelBag.sharedDisposelBag.disposeBag)

Do i need to modify it so that there is a [weak self] capture list at the beginning of the closure? Like this:

    searchController.searchBar.rx_text.throttle(0.2, scheduler: MainScheduler.instance).subscribeNext { [weak self] searchText in
        self?.viewModel.searchForLocation(searchText)
    }.addDisposableTo(DisposelBag.sharedDisposelBag.disposeBag)
Pramod More
  • 1,220
  • 2
  • 22
  • 51
Rohan Panchal
  • 1,211
  • 1
  • 11
  • 28

4 Answers4

23

If the closure is not owned by the class you do not have to use [weak self].

In the case of in-line closures the closure is not owned by the class but by the scope it is in and will be released when the scope is left.

If the closure is passed in it may or may not be owned by the class (a property for example) and it is prudent to use [weak self] incase it is owned by the class.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • 7
    your answer is a bit confusing. If the closure was owned by the scope, e.g a function, the closure would never be called after that function is called. The closure is owned by the object you're observing, in that case `searchBar`. So if somehow `self` has a strong ref to `searchBar` OP should use `weak` – streem Dec 05 '16 at 02:41
  • Can you give some example? – Jack Guo Jul 30 '18 at 13:46
20

Yes, you should create a weak capture of self if you access self within the closure and it is possible that self could become nil before the closure is called.

If a closure captures self and then self becomes nil, when the closure is called and attempts to access that self, you’ll get an exception.

Credit to scotteg, he has an example project on GitHub: https://github.com/scotteg/TestRxSwiftClosures

See the DetailViewController in the example.

You can uncomment the other two examples, one at a time, to see the results. The first one doesn’t define a capture list at all, and the second one defines an unowned capture. Run the app and enter some text and tap Done within 5 seconds (there’s a 5-second delay in each closure). The first two examples will result in exceptions being thrown.

The basic rule is this: If the capture (e.g., self) can be set to nil, such as if the instance it references gets deallocated, define the capture as weak. Otherwise, if a closure and a capture within that closure will ​always​ refer to each other and be deallocated at the same time, define the capture as unowned.

Son Nguyen
  • 578
  • 3
  • 10
  • This example is quite misleading, because RxSwift is not the problem in this case, but rather the `Async.main` call. If you were to you use the `delay` operator from RxSwift, you would not have this problem and you could use `[unowned self]` safely. – Lord Zsolt Apr 03 '21 at 17:25
2

You'll want to use [unowned self] or [weak self] if there will be a strong reference cycle. Variables inside closures can be "owned" by the closure and will stick around if the closure is, so that's why we do [unowned self] or [weak self].

jasonnoahchoi
  • 871
  • 5
  • 16
1

[unowned self] means self cannot be nil when block gets called.if block gets called and self is nil,then app crash.

[weak self] means self can be nil when block gets called.As a matter of that,you have to handle optional self inside the block.

SO,My quick answer is 1.when you are referring a view model in a view controller block,always use [unowned self] because you can ensure view model always exists in its associated view controller.

2.in other cases,always be alerted when you use self in a block.choose unowned vs weak based on if self can be nil or not.

Charlie Cai
  • 281
  • 1
  • 6