title: Get one value from LiveData
body: <p>I have <code>LiveData</code> for Books in <code>ViewModel</code>'s constructor:</p>
  
  <pre><code>LiveData&lt;List&lt;Book&gt;&gt; books;
  
  public MyViewModel(@NonNull Application application) {
      super(application);
      books = bookRepository.getBooks();
  }
  </code></pre>
  
  <p>When user creates new <code>book</code> from UI, I want attribute <code>book_order</code> to be filled with incremented maximum of <code>book_order</code> of other books. To better describe what I want, see following preudocode:</p>
  
  <pre><code>book.book_order = max(books.book_order) + 1;
  </code></pre>
  
  <p>So when there are three books with <code>book_order</code> 1, 2, 3 respectively, new book would have this attribute set to 4.</p>
  
  <p>Question is, how can I do this with <code>LiveData</code> in <code>ViewModel</code>? I tried using <code>Transformations.map</code> to the new <code>LiveData</code>, but this approach is not working at all, <code>bookMax</code> seems to be <code>null</code>.</p>
  
  <pre><code>public void insertBook(Book book) {
      LiveData&lt;Integer&gt; bookMax = Transformations.map(books,  
          list -&gt; {
            int value = 0;
            for(Book b: list) {
                if (value &lt; b.getBookOrder()) {
                   value = b.getBookOrder();
                }
            }
          }
      );
      book.setBookOrder(bookMax + 1)
      bookRepository.update(book);
  }
  </code></pre>
  
  <p>Any ideas how to set incremented maximum to the new book? It can be another approach than the one described here. <code>ViewModel</code> was created to separate app logic from UI. However it does not seem to do that in this case, because if I want to observe value, I need to be in <code>Activity</code>. Also, I did not find any alternative how to do this kind of getting one value from DB. Any help appreciated.</p>