title: Need help to wrap my head around the concept of retry and share in RxSwift
body: <p>so I can't really wrap my head around the concept of <code>retry()</code> and <code>share()</code> function in an <code>RxSwift.Observable</code>. Well, I think I may have some ideas about what they are but a certain cases got me questioning my understanding about them. So I have this test case:</p>
  <pre><code>func testOnViewDidLoad_WhenError_ShouldRetry3Times() throws {
      var actualRetryCount = 0
      let creditCardInfoProvider: CreditCardInfoProviderBlock = {
          actualRetryCount += 1
          return .error(RxCocoaURLError.unknown)
      }
      
      let viewModel = createViewModel(creditCardInfoProvider: creditCardInfoProvider)
      
      viewModel.onViewDidLoad()
      
      XCTAssertEqual(3, actualRetryCount)
  }
  </code></pre>
  <p>The class is:</p>
  <pre><code>final class PaymentInfoViewModel {
      private(set) lazy var populateData = creditCardsRelay.asDriver()
      private let creditCardsRelay = BehaviorRelay&lt;[CreditCardInfo]&gt;(value: [])
  
      private let creditCardInfoProvider: () -&gt; Observable&lt;[CreditCardInfo]&gt;
      init(creditCardInfoProvider: @escaping () -&gt; Observable&lt;[CreditCardInfo]&gt;) {
          self.creditCardInfoProvider = creditCardInfoProvider
      }
  
      func onViewDidLoad() {
          .....
      }
  }
  </code></pre>
  <p>My first question is: How come this works?</p>
  <pre><code>func onViewDidLoad() {
      Observable.just(())
          .flatMapLatest { [weak self] () -&gt; Observable&lt;[CreditCardInfo]&gt; in
              guard let `self` = self else { return .empty() }
              return self.creditCardInfoProvider() }
          .retry(3)
          .do(onError: { [weak self] in
              self?.handleErrors(error: $0)
          })
          .bind(to: creditCardsRelay)
          .disposed(by: disposeBag)
  }
  </code></pre>
  <p>But this doesn't (the result was one. Meaning it didn't get retried.)?</p>
  <pre><code>func onViewDidLoad(){
      creditCardInfoProvider()
          .retry(3)
          .do(onError: { [weak self] in
              self?.handleErrors(error: $0)
          })
          .bind(to: creditCardsRelay)
          .disposed(by: disposeBag)
  }
  </code></pre>
  <p>My second question is: I have another relay that will be triggered by the same function. So I refactored it to be a shared observable like this:</p>
  <pre><code>func onViewDidLoad() {
      let sharedCardProvider = Observable.just(())
          .flatMapLatest { [weak self] () -&gt; Observable&lt;[CreditCardInfo]&gt; in
              guard let `self` = self else { return .empty() }
              return self.creditCardInfoProvider() }
          .retry(3)
          .do(onError: { [weak self] in
              self?.handleErrors(error: $0)
          }).share()
          
      sharedCardProvider
          .bind(to: creditCardsRelay)
          .disposed(by: disposeBag)
      
      sharedCardProvider
          .map { !$0.isEmpty }
          .bind(to: addCreditCardButtonHiddenRelay)
          .disposed(by: disposeBag)
  }
  </code></pre>
  <p>The thing is, the test becomes red with the result of <code>actualRetryCount</code> is 6. Deleting the <code>share()</code> function returned the same value (ie. 6 retries). So, that means it got called twice like a normal observable, not a shared one. Why does that happen?</p>
  <p>For now, what I did was putting the emitting of the second relay in a <code>.do(onNext:)</code> block so that's not really the problem here. I am just confused about the behavior.</p>
  <p>Thanks in advance.</p>