Oscar Edmit
Oscar Edmit

Reputation: 46

Objectify not returns new data

I'm using Ojectify to store datastore, I have an entity that when I keep it if the changes are saved, but in the browser to display it sometimes shows me the previous data and sometimes the new data.

@Entity 
public class BrandDto {
    @Id
    private Long id;
    @Index
    private String name;

    @Index
    private List<Ref<UserDto>> users;

    @Index
    private Integer numeroFollowers;

    getters and setters ..... 
}

It happens in users and numeroFollowers fields.

I update this data as follows:

UserDto user = ofy().load().type(UserDto.class).id(p_userId).now();
if (user != null) {
    BrandDto brand = ofy().load().type(BrandDto.class).id(p_brandId).now(); //get(p_brandId);
    List<UserDto> users = brand.getUsers() != null ? brand.getUsers() : new ArrayList<UserDto>();
    if (!users.contains(user)) {
       users.add(user);
    }
    brand.setUsers(users);
    brand.setNumeroFollowers(users.size());
    ofy().save().entity(brand).now();
    return true;
} else {
    return false;
}

And I read as follows:

List<BrandDto> brands = ofy().load().type(BrandDto.class).list();

Other query that I use:

UserDto user = ofy().load().type(UserDto.class).id(p_userId).now();
Ref<UserDto> ref = Ref.create(user);
List<BrandDto> brands = ofy().load().type(BrandDto.class).filter("users", ref).list();

Upvotes: 0

Views: 97

Answers (1)

stickfigure
stickfigure

Reputation: 13556

While get-by-key operations (like load().type(...).id(...)) are strongly consistent by default, queries are eventually consistent.

Here's more information: https://cloud.google.com/developers/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/

Upvotes: 2

Related Questions