8

我正在尝试保存一个对象并验证它是否在之后立即保存,并且它似乎没有工作。

这是我的对象

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”对象为空。即使我刚刚完成保存它。我究竟做错了什么?

--更新-- 如果我稍后尝试(比如几分钟后),有时我确实会看到该对象,但不是在保存之后。这是否与高复制存储有关?

4

1 回答 1

7

前段时间有同样的行为,并在谷歌群组上提出了一个问题 - 客观化

这是我得到的答案:

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 

我也得到了另一个很好的答案来处理这种行为

对于删除,查询键,然后批量获取实体。确保您的获取设置为强一致性(尽管我相信这是默认设置)。batch-get 应该为已删除的实体返回 null。添加时,它变得有点棘手。索引更新可能需要几秒钟。AFAIK,有三种方法可以解决这个问题:1;使用预先计算的结果(完全避免查询)。如果您的下一个视图是用户最近创建的实体,请在用户实体中保留这些键的列表,并在创建新实体时更新该列表。该列表将始终是新鲜的,无需查询。除了避免过时的索引之外,这还可以加快您的应用程序。您可以可靠地管理的结果集越多,您可以避免的查询就越多。

2;通过使用最近添加的实体“增强”查询结果来隐藏延迟。根据您添加实体的速度,要么仅注入最新的密钥,要么将其与1中的解决方案结合使用。

3;在登陆基于查询的视图之前,通过让用户浏览一些未受影响的视图来隐藏延迟。这个策略肯定有味道。您需要确保这些额外的步骤与用户相关,否则您将获得糟糕的体验。

蝴蝶,乔金

你可以在这里阅读所有内容:

如果我在删除一个对象后不使用异步 api,我仍然会在删除后立即完成的查询中得到它,或者在我添加一个对象后没有立即得到它


类似问题的另一个好答案Objectify does not store synchronously, even with now

于 2013-02-01T19:06:58.037 回答