Note that there are some explanatory texts on larger screens.

plurals
  1. POObjectify - how do I ensure that my objects are synchronized
    text
    copied!<p>I have a simple web service, which offers two methods:</p> <p><strong>addHighscore</strong> - adds new entry to highscore if either MAX is not reached or score is higher then least entry</p> <pre><code>// check if free place in highscore int count = ofy().load().type(Highscore.class) .filter("difficulty", difficulty).count(); boolean insert = count + 1 &lt; MAX; if(insert == false) { // get last element and free if possible Highscore least = ofy().load().type(Highscore.class) .filter("difficulty", difficulty).order("-score").order("-date") .first().now(); if(highscore.getScore() &gt; least.getScore()) { ofy().delete().entity(least).now(); insert = true; } } // insert if(insert == true) ofy().save().entity(highscore).now(); </code></pre> <p><strong>getHighscore</strong> - get highscore</p> <pre><code>List&lt;Highscore&gt; list = ofy().load().type(Highscore.class) .filter("difficulty", difficulty).order("score DESC").list(); </code></pre> <p>Now my problem is the following: How do I ensure that 1) call to addHighscore sees the result of the previous call 2) a client that calls addHighscore and then getHighscore sees his results</p> <p>I read the explanation at <a href="https://developers.google.com/appengine/docs/java/datastore/structuring_for_strong_consistency" rel="nofollow">https://developers.google.com/appengine/docs/java/datastore/structuring_for_strong_consistency</a> but i have no clue how i could apply that to my own example. What could I use as ancestor key?</p> <p>Can somebody explain me how I can correct this? (If possible with pseudycode samples)</p> <p><strong>UPDATE</strong></p> <p>I remodelled my addHighscore function which now looks like this</p> <pre><code> boolean insert = ofy().transact(new Work&lt;Boolean&gt;() { @Override public Boolean run() { // run sync Key&lt;Sync&gt; ancestor = Sync.getKey(); if(ofy().load().key(ancestor).now() == null) ofy().save().entities(new Sync()).now(); // check if free place in highscore int count = ofy().load().type(Highscore.class).ancestor(ancestor).filter("difficulty", difficulty).order("-score").order("date").count(); boolean insert = count &lt; MAX; if(insert == false) { // get last element and free if possible Highscore least = ofy().load().type(Highscore.class).ancestor(ancestor).filter("difficulty", difficulty).order("-score").order("date").first().now(); // order DESC with - if(highscore.getScore() &gt; least.getScore()) { ofy().delete().entity(least).now(); insert = true; } } // insert if(insert == true) ofy().save().entity(highscore).now(); return insert; } }); </code></pre> <p>with sync</p> <pre><code>@Entity public class Sync { @Id long id = 1; public static Key&lt;Sync&gt; getKey() { return Key.create(Sync.class, 1); } } </code></pre> <p>and furthermore I added a @Parent annotation to Highscore via <code>@Parent Key&lt;Sync&gt; parent = Sync.getKey()</code></p> <p>This seems to work - I wonder if this is a proper solution. Or is there a better way to do it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload