我正在尝试保存一个对象并验证它是否在之后立即保存,但它似乎不起作用。
这是我的对象
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
@Entity
public class PlayerGroup {
@Id public String n;//sharks
public ArrayList<String> m;//members [39393,23932932,3223]
}
这是保存然后尝试立即加载的代码。
playerGroup = new PlayerGroup();
playerGroup.n = reqPlayerGroup.n;
playerGroup.m = reqPlayerGroup.m;
ofy().save().entity(playerGroup).now();
response.i = playerGroup;
PlayerGroup newOne = ofy().load().type(PlayerGroup.class).id(reqPlayerGroup.n).get();
但是“newOne”对象是空的。即使我刚刚完成保存。我做错了什么?
--更新-- 如果我稍后再试(比如几分钟后),有时我确实会看到该对象,但不是在保存后立即看到的。这与高复制存储有关吗?
最佳答案
前段时间有过同样的行为,并在 google groups - objectify 上问了一个问题
这里是我得到的答案:
You are seeing the eventual consistency of the High-Replication
Datastore. There has been a lot of discussion of this exact subject
on the Objecify list in google groups , including several links to the
Google documentation on the subject.
Basically, any kind of query which does not include an ancestor() may
return results from a stale view of the datastore.
Jeff
我也得到了另一个处理行为的好答案
For deletes, query for keys and then batch-get the entities. Make sure your gets are set to strong consistency (though I believe this is the default). The batch-get should return null for the deleted entities. When adding, it gets a little trickier. Index updates can take a few seconds. AFAIK, there are three ways out of this: 1; Use precomputed results (avoiding the query entirely). If your next view is the user's recently created entities, keep a list of those keys in the user entity, and update that list when a new entity is created. That list will always be fresh, no query required. Besides avoiding stale indexes, this also speeds up your app. The more you result sets you can reliably manage, the more queries you can avoid.
2; Hide the latency by "enhancing" the query results with the recently added entities. Depending on the rate at which you're adding entities, either inject only the most recent key, or combine this with the solution in 1.
3; Hide the latency by taking the user through some unaffected views before landing on your query-based view. This strategy definitely has a smell over it. You need to make sure those extra steps are relevant to the user, or you'll give a poor experience.
Butterflies, Joakim
您可以在这里阅读所有内容:
类似问题的另一个好答案:Objectify doesn't store synchronously, even with now
关于google-app-engine - 不使用 Objectify 和 GAE 保存的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14651998/