Why .now()? (Objectify)

前端 未结 3 1840
太阳男子
太阳男子 2021-02-06 12:35

Why would I ever want to load an Objectify entity asynchronously? And what does asynchronous loading actually mean?

According to Objectify documentation about loading,

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-06 13:10

    Asynchronous operations start a network fetch to the backend and then let your code continue executing. The advantage of async operations is that you can run several of them in parallel:

    Result th1 = ofy().load().key(thingKey1);
    Result th2 = ofy().load().key(thingKey2);
    Result th3 = ofy().load().key(thingKey3);
    th1.now();
    th2.now();
    th3.now();
    

    This executes significantly faster than calling now() immediately each time. Note this is a bad example because you probably would use a batch get (which also parallelizes the operation) but you can have several queries, saves, deletes, etc running simultaneously.

    now() always forces synchronous completion, blocking until done.

提交回复
热议问题