sergdort / CleanArchitectureRxSwift

Example of Clean Architecture of iOS app using RxSwift
MIT License
3.88k stars 494 forks source link

Retain cycle? #41

Open Sajjon opened 6 years ago

Sajjon commented 6 years ago

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 commented 6 years ago

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?

sergdort commented 6 years ago

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