Clarify how GWT RequestFactory and RequestContext work

二次信任 提交于 2019-12-02 16:53:37

Take a look at the RequestFactoryTest in the GWT source code for examples. The testChangedEdit() method is similar to what you're trying to write. It calls a find() method and then operates on the returned proxy in the onSuccess() method.

A RequestContext isn't a long-lived object. It is only valid from the time that it is called to when you call fire() on it. It can be re-used only if the onFailure() or onViolation() method is called in your Receiver.

An EntityProxy or ValueProxy returned via Receiver.onSuccess() represents a snapshot of server data. Thus, the proxy is immutable unless it is associated with a RequestContext by calling edit(). The proxies returned by RequestContext.create() are mutable. A mutable proxy is always associated with exactly one RequestContext and it is an error to "cross the streams." It is not an error to re-edit() a mutable proxy.

The reason it works this way is to allow the RequestFactory client to only send deltas to the server. The deltas are applied to the long-lived entities on the server by calling the domain object's find() method (or using a Locator). The RequestContext is essentially an accumulator for proxy.setFoo() calls and one or more Request / InstanceRequest invocations.

General guidelines:

  • Don't store RequestContext instances in fields of objects whose lifetime exceeds that of the fire() method invocation.
  • Similarly, editable EntityProxy or ValueProxy instances should not be retained beyond the call to fire().
  • The EntityProxyId returned from EntityProxy.stableId() can be retained indefinitely, even from a newly-created proxy. The stableId object is suitable for use as the key in Map objects and has stable object-identity semantics (i.e. two snapshots of the same server domain object with different versions would return the same `EntityProxyId').
  • Instances of RequestFactory should be constructed once and retained for the lifetime of the module, since they have non-trivial construction cost.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!