code: Observable<ComapiResult<MessagesQueryResponse>> processOrphanedEvents(ComapiResult<MessagesQueryResponse> result, final ChatController.OrphanedEventsToRemoveListener removeListener) {
  
          if (result.isSuccessful() && result.getResult() != null) {
  
              final MessagesQueryResponse response = result.getResult();
  
              final List<MessageReceived> messages = response.getMessages();
              final String[] ids = new String[messages.size()];
  
              if (!messages.isEmpty()) {
                  for (int i = 0; i < messages.size(); i++) {
                      ids[i] = messages.get(i).getMessageId();
                  }
              }
  
              return db.save(response.getOrphanedEvents())
                      .flatMap(count -> db.queryOrphanedEvents(ids))
                      .flatMap(toDelete -> Observable.create(emitter ->
                              storeFactory.execute(new StoreTransaction<ChatStore>() {
  
                                  @Override
                                  protected void execute(ChatStore store) {
  
                                      if (!toDelete.isEmpty()) {
  
                                          storeFactory.execute(new StoreTransaction<ChatStore>() {
                                              @Override
                                              protected void execute(ChatStore store) {
  
                                                  List<ChatMessageStatus> statuses = modelAdapter.adaptEvents(toDelete);
  
                                                  store.beginTransaction();
  
                                                  if (!statuses.isEmpty()) {
                                                      for (ChatMessageStatus status : statuses) {
                                                          store.update(status);
                                                      }
                                                  }
  
                                                  String[] ids = new String[toDelete.size()];
                                                  for (int i = 0; i < toDelete.size(); i++) {
                                                      ids[i] = toDelete.get(i).id();
                                                  }
                                                  removeListener.remove(ids);
  
                                                  store.endTransaction();
  
                                                  emitter.onNext(result);
                                                  emitter.onCompleted();
                                              }
                                          });
                                      } else {
                                          emitter.onNext(result);
                                          emitter.onCompleted();
                                      }
                                  }
                              }), Emitter.BackpressureMode.BUFFER));
          } else {
              return Observable.fromCallable(() -> result);
          }
      }
docs: Handle orphaned events related to message query.
  
  @param result Message query response
  @return Observable returning message query response from argument unchanged by the method.