io.objectbox.query.Query.subscribe()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(5.8k)|赞(0)|评价(0)|浏览(135)

本文整理了Java中io.objectbox.query.Query.subscribe方法的一些代码示例,展示了Query.subscribe的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.subscribe方法的具体详情如下:
包路径:io.objectbox.query.Query
类名称:Query
方法名:subscribe

Query.subscribe介绍

[英]A io.objectbox.reactive.DataObserver can be subscribed to data changes using the returned builder. The observer is supplied via SubscriptionBuilder#observer(DataObserver) and will be notified once the query results have (potentially) changed.

With subscribing, the observer will immediately get current query results. The query is run for the subscribing observer.

Threading notes: Query observers are notified from a thread pooled. Observers may be notified in parallel. The notification order is the same as the subscription order, although this may not always be guaranteed in the future.

Stale observers: you must hold on to the Query or io.objectbox.reactive.DataSubscription objects to keep your DataObservers active. If this Query is not referenced anymore (along with its io.objectbox.reactive.DataSubscriptions, which hold a reference to the Query internally), it may be GCed and observers may become stale (won't receive anymore data).
[中]木卫一。objectbox。反应性。DataObserver可以使用返回的生成器订阅数据更改。observer通过SubscriptionBuilder#observer(DataObserver)提供,一旦查询结果(可能)发生更改,就会收到通知。
通过订阅,观察者将立即获得当前的查询结果。查询针对订阅的观察者运行。
线程注释:查询观察者从线程池中得到通知。观察员可同时收到通知。通知顺序与订阅顺序相同,尽管这在将来可能并不总是得到保证。
陈旧的观察者:你必须保持查询或io。objectbox。反应性。DataSubscription对象以保持DataObservators处于活动状态。如果不再引用此查询(以及其io.objectbox.reactive.DataSubscriptions,它们在内部保存对查询的引用),则可能会对其进行GCed,并且观察者可能会变得陈旧(不再接收数据)。

代码示例

代码示例来源:origin: objectbox/objectbox-java

/**
 * Convenience for {@link #subscribe()} with a subsequent call to
 * {@link SubscriptionBuilder#dataSubscriptionList(DataSubscriptionList)}.
 *
 * @param dataSubscriptionList the resulting {@link io.objectbox.reactive.DataSubscription} will be added to it
 */
public SubscriptionBuilder<List<T>> subscribe(DataSubscriptionList dataSubscriptionList) {
  SubscriptionBuilder<List<T>> subscriptionBuilder = subscribe();
  subscriptionBuilder.dataSubscriptionList(dataSubscriptionList);
  return subscriptionBuilder;
}

代码示例来源:origin: objectbox/objectbox-java

@Override
  public void subscribe(final SingleEmitter<List<T>> emitter) throws Exception {
    query.subscribe().single().observer(new DataObserver<List<T>>() {
      @Override
      public void onData(List<T> data) {
        if (!emitter.isDisposed()) {
          emitter.onSuccess(data);
        }
      }
    });
    // no need to cancel, single never subscribes
  }
});

代码示例来源:origin: objectbox/objectbox-java

@Override
  public void subscribe(final ObservableEmitter<List<T>> emitter) throws Exception {
    final DataSubscription dataSubscription = query.subscribe().observer(new DataObserver<List<T>>() {
      @Override
      public void onData(List<T> data) {
        if (!emitter.isDisposed()) {
          emitter.onNext(data);
        }
      }
    });
    emitter.setCancellable(new Cancellable() {
      @Override
      public void cancel() throws Exception {
        dataSubscription.cancel();
      }
    });
  }
});

代码示例来源:origin: objectbox/objectbox-java

static <T> void createListItemEmitter(final Query<T> query, final FlowableEmitter<T> emitter) {
  final DataSubscription dataSubscription = query.subscribe().observer(new DataObserver<List<T>>() {
    @Override
    public void onData(List<T> data) {
      for (T datum : data) {
        if (emitter.isCancelled()) {
          return;
        } else {
          emitter.onNext(datum);
        }
      }
      if (!emitter.isCancelled()) {
        emitter.onComplete();
      }
    }
  });
  emitter.setCancellable(new Cancellable() {
    @Override
    public void cancel() throws Exception {
      dataSubscription.cancel();
    }
  });
}

代码示例来源:origin: objectbox/objectbox-java

public MockQuery(boolean hasOrder) {
  // box = mock(Box.class);
  // boxStore = mock(BoxStore.class);
  // when(box.getStore()).thenReturn(boxStore);
   query = mock(Query.class);
  fakeQueryPublisher = new FakeQueryPublisher();
  SubscriptionBuilder subscriptionBuilder = new SubscriptionBuilder(fakeQueryPublisher, null, null);
  when(query.subscribe()).thenReturn(subscriptionBuilder);
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testObserver() {
  int[] valuesInt = {2003, 2007, 2002};
  Query<TestEntity> query = box.query().in(simpleInt, valuesInt).build();
  assertEquals(0, query.count());
  query.subscribe().observer(this);
  assertLatchCountedDown(latch, 5);
  assertEquals(1, receivedChanges.size());
  assertEquals(0, receivedChanges.get(0).size());
  receivedChanges.clear();
  latch = new CountDownLatch(1);
  putTestEntitiesScalars();
  assertLatchCountedDown(latch, 5);
  assertEquals(1, receivedChanges.size());
  assertEquals(3, receivedChanges.get(0).size());
}

代码示例来源:origin: objectbox/objectbox-java

@Test
public void testSingle() throws InterruptedException {
  putTestEntitiesScalars();
  int[] valuesInt = {2003, 2007, 2002};
  Query<TestEntity> query = box.query().in(simpleInt, valuesInt).build();
  query.subscribe().single().observer(this);
  assertLatchCountedDown(latch, 5);
  assertEquals(1, receivedChanges.size());
  assertEquals(3, receivedChanges.get(0).size());
  receivedChanges.clear();
  putTestEntities(1);
  Thread.sleep(20);
  assertEquals(0, receivedChanges.size());
}

代码示例来源:origin: objectbox/objectbox-java

final List<Integer> receivedSums = new ArrayList<>();
query.subscribe().transform(new DataTransformer<List<TestEntity>, Integer>() {

代码示例来源:origin: owntracks/android

@Override
public void onStart() {
  super.onStart();
  if(this.subscription == null || this.subscription.isCanceled()) {
    this.subscription = viewModel.getWaypointsList().subscribe().on(AndroidScheduler.mainThread()).observer(recyclerViewAdapter) ;
  }
}

代码示例来源:origin: objectbox/objectbox-examples

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  setUpViews();
  notesBox = ((App) getApplication()).getBoxStore().boxFor(Note.class);
  // query all notes, sorted a-z by their text (https://docs.objectbox.io/queries)
  notesQuery = notesBox.query().order(Note_.text).build();
  // Reactive query (https://docs.objectbox.io/data-observers-and-rx)
  notesQuery.subscribe(subscriptions).on(AndroidScheduler.mainThread())
      .observer(new DataObserver<List<Note>>() {
        @Override
        public void onData(List<Note> notes) {
          notesAdapter.setNotes(notes);
        }
      });
}

相关文章