Chapter 4. Resource counting

In your example with saving photo in SavedPhotoAlbum using Observable I do small experiment - save my collages several times and I found, that RxSwift.Resources.total always increase!

resources: 6
completed photo selection
resources: 8
completed photo selection
resources: 9
completed photo selection
resources: 10

But when I remove .addDisposableTo(bag) it will be fine and clear all resources.

resources: 6
completed photo selection
resources: 8
completed photo selection
resources: 8
completed photo selection
resources: 8

Question.
What are the best practice? In one case bag allow dispose when controller dealloc, in other case, when controller live long time we have leaks!

2 Likes

I’m having this same issue. Does anybody have a reason for this? My assumption is that if you complete this observable (and I’ve added a disposable callback that is firing so it is being “disposed”), you will NOT have a resources issue. I also noticed as well if you remove the .addDisposableTo(bag) call the resources clear.

This chapter is not about best practices - it’s one of the first chapters in the book and tries to introduce you to some of the basics (I’m sure the text mentions this few times). As you see in the chapter I intentionally drive to readers to “creating the problem” (e.g. not disposing the subscription properly) so that I can introduce resource counting.

I’m just trying to clear this up for myself, I understand that (and like you said it’s been mentioned) a lot of the early stuff is not best practices, but I want to make sure I understand how disposing works with observables that have been “completed.”

Here is the code that the MainViewController has:

photosViewController.selectedPhotos
      .subscribe(onNext: { [weak self] newImage in
        guard let images = self?.images else { return }
        images.value.append(newImage)
      }, onDisposed: {
        print("completed photo selection")
      })
      .addDisposableTo(photosViewController.bag)

And here is the code that the PhotosViewController has:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    selectedPhotosSubject.onCompleted()
  }

Just to be clear, even if I “complete” the observable when the viewWillDisappear is called, I still need to add the observer to the photosViewController.bag for this to be disposed properly.

The way that I read explanation in the book was that adding the selectedPhotosSubject.onCompleted() was something you did instead of adding the observer to the photosViewController.bag, not in addition to it, as it was described as a “slightly more elegant solution.” Thanks for the quick reply by the way.

1 Like

I have this same question! I think the text is definitely a bit confusing here.

Hello, I’ve faced to the same issue recently.

And I understood, that code .addDisposableTo(photosViewController.bag) can be omitted.
I have tested with RxSwift.Recources.total and there were no memory leaks.
But after this change Xcode warns about unused variable. So you should use _ = ... statement like this:

_ = photosVC.selectedPhotos
  .subscribe(
    onNext: { [weak self] newImage in
      guard let images = self?.images else { return }
      images.value.append(newImage)
    },
    onDisposed: {
      print("completed photo selection")
  })
1 Like