rx-swift,  swift,  ios

How to avoid memory leaks

RxSwift is a powerful framework and, as always, with power comes responsibility.

How to avoid memory leaks

RxSwift is a powerful framework and, as always, with power comes responsibility. In RxSwift the memory is usually managed through an object called a DisposeBag.

For instance, every time we subscribe to an Observable, RxSwift makes sure the subscription is kept in memory until either a .completed or .error event is received or the subscription is being garbage collected by the DisposeBag.

It is fairly easy to make a mistake and to add the subscription to a wrong DisposeBag. For example, to add it to the controller’s bag instead of the cell’s. Mistakes happen and to catch them early it is a good idea to add a resource counting mechanism to each view controller. The mechanism will make sure resources allocated during the lifetime of the view controller are released and if no, will output an error.

Infinum provides a default implementation of such a mechanism.

Subscribe to The infinite monkey theorem

Get the latest posts delivered right to your inbox