title: How to return a Maybe or result from Maybe
body: <p>I'm on an Usecase and I have a repository, where I have all of the methods that I have to do, for instance : </p>
  
  <pre><code>class MyUseCase(
      private val myRepository: MyRepository
  ) {
  
      fun execute(example: String) { //First I don't know what to return here
      //Here I have to check if theres anything on the room db, for this I have this method
      myRepository.findLocally(example) // it returns a Maybe&lt;MyObject&gt;
      //Then if it returns an object I have to do another call that is 
      myRepository.findObject(example) //it will return a Maybe&lt;List&lt;OtherObject&gt;&gt;
      //So if we arrive to this, the usecase is done and I should return the last result.
      //if user doesn't search it before then I have to do an api call using
      myRepository.getFromApi(example) //it will return Single&lt;List&lt;OtherObject&gt;&gt;
      //When I get the result then I have to do two inserts using 
      myRepository.insertWord(example) : Completable
      //And then I want to add all the result from the api call it can be a List&lt;MyObject&gt; or a []
      myRepository.insertList(resultFromApi) : Completable
  
      }
  
  }
  </code></pre>
  
  <p>My problem is that I do now know how to execute all of this or if I should split it or something, this is called from my presenter, so I want to return to my presenter the data to call the view.showData() for instance.</p>
  
  <p>The idea, should execute it like this : </p>
  
  <pre><code>if(myRepository.findLocally(example)) {
    return myRepository.findObject(example)
  }
  else{
    myRepository.getFromApi(example)
    myRepository.insertWord(example)
    myRepository.insertList(resultFromApi)
  }
  
  
  </code></pre>
  
  <h2>Edit</h2>
  
  <p>I'm looking the way to do it using RxJava.</p>
  
  <p>This is how I'm trying to do it : </p>
  
  <pre><code>return myRepository.findLocally(example).flatMap {
              myRepository.findObject(example)
          }.switchIfEmpty {
              myRepository.getFromApi(example).flatMap { example -&gt;
                  example.flatMap {
                      myRepository.insertList(
                          MyExample(
                              it.id,
                              it.name,
                              it.description
                          )
                      )
                  }
                  myRepository.insertWord(example)
  
              }
          }
  </code></pre>