CleanArchitectureRxSwift icon indicating copy to clipboard operation
CleanArchitectureRxSwift copied to clipboard

Retain cycle?

Open Sajjon opened this issue 6 years ago • 2 comments

Hey! Awesome project! Your code is so inspiring!

I just have one short question, in your ViewModels transform:input method you do use capture list to weakify self, e.g. in EditPostViewModel

        let savePost = saveTrigger.withLatestFrom(post)
                .flatMapLatest { post in
                    return self.useCase.save(post: post)
                            .trackError(errorTracker)
                            .asDriverOnErrorJustComplete()
                }

You use self.useCase.save, isn't this a retain cycle? Dont we have to use:

        let savePost = saveTrigger.withLatestFrom(post)
                .flatMapLatest { [weak self] post in
                    guard let `self` = self else { return someStuff }
                    return self.useCase.save(post: post)
                            .trackError(errorTracker)
                            .asDriverOnErrorJustComplete()
                }

Thanks!

Sajjon avatar Feb 25 '18 18:02 Sajjon

I saw that you actually use capture list in CreatePostViewModel:

.flatMapLatest { [unowned self] in
                    return self.createPostUseCase.save(post: $0)
                            .trackActivity(activityIndicator)
                            .asDriverOnErrorJustComplete()
                }

But you use unowned self which would result in a crash if it were to be deallocated right?

Sajjon avatar Feb 28 '18 12:02 Sajjon

Hi, @Sajjon

Regarding:

I just have one short question, in your ViewModels transform:input method you do use capture list to weakify self, e.g. in EditPostViewModel

I do not think it's a retain cycle as we do not keep reference to the savePost observable. It's retained by the disposeBag in the ViewController which will release everything on deinit. So there is no need even for unowned in the CreatePostViewModel

sergdort avatar Mar 04 '18 16:03 sergdort