Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'[weak self]' in RXSwift closures

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)
like image 423
Rohan Panchal Avatar asked Jan 25 '16 22:01

Rohan Panchal


4 Answers

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.

like image 70
zaph Avatar answered Nov 01 '22 21:11

zaph


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.

like image 35
Son Nguyen Avatar answered Nov 01 '22 21:11

Son Nguyen


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].

like image 2
jasonnoahchoi Avatar answered Nov 01 '22 21:11

jasonnoahchoi


[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.

like image 1
Charlie Cai Avatar answered Nov 01 '22 21:11

Charlie Cai