Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems iterating through Foreign Collections with ORMLite
    text
    copied!<p>Am having an issue trying to use Ormlite <code>ForeignCollection</code> with the following classes. I believe I have my Run &amp; Waypoint classes correctly annotated. (Run has a collection of WayPoints). However when I attempt to access WayPoints (see third code extract) my <code>CloseableIterator</code> returns null when I call <code>hasNext()</code>. When I test this out with the emulator I first generate a number of WayPoints for the Run and store them. I then try to access those WayPoints through an instance of Run. Just to complete the picture without dumping too much code here, the <code>ForeignCollection&lt;WayPoint&gt;</code> object is passed to the Contender constructor by calling </p> <pre><code>run.getWayPoints(); </code></pre> <p>Here's how I am creating a WayPoint and storing it :: - When GPSLocation.onLocationChanged() fires and the state is GPS_STATE_RUNNING, then a call to createWayPoint() is made. This instantiates a WayPoint, sets lng/lat values and also sets the handle to the associated Run instance, which at this point has not yet been stored itself to the db. An Intent is sent to another class that calls back in on GPSLocation.updateWayPointWithElapsedTime() where the WayPoint is finally stored to the db. When a run completes an external class calls storeRun() which stores the Run instance. I am beginning to wonder if storing a WayPoint when it's associated Run instance is itself not yet stored is part of the problem. </p> <p>** Note :: the classes will not compile as they are cutdown to show sections relavent to the question.</p> <pre><code>@DatabaseTable public class Run { @DatabaseField(generatedId=true) private int id; @DatabaseField(foreign=true,foreignAutoRefresh=true) private Route route; @ForeignCollectionField private ForeignCollection&lt;WayPoint&gt; wayPoints; @DatabaseField private Date date; @DatabaseField private long time; public ForeignCollection&lt;WayPoint&gt; getWayPoints() { return wayPoints; } } @DatabaseTable public class WayPoint { @DatabaseField(generatedId=true) private int id; @DatabaseField private int latitude; @DatabaseField private int longitude; @DatabaseField private int time; // offset @DatabaseField(foreign=true,foreignAutoRefresh=true) private Run run; } public class Contender implements Comparable&lt;Contender&gt; { private int accumulatedDistance; private int interpolatedDistance; private WayPoint startPoint; CloseableIterator itr; private int id; WayPoint previous; WayPoint next; public Contender(ForeignCollection&lt;WayPoint&gt; wayPoints, WayPoint startPoint, int id) { this.startPoint = startPoint; this.id = id; itr = wayPoints.closeableIterator(); if(itr.hasNext()) { next = (WayPoint)itr.next(); } else { Log.e("@@@", "no way points"); } Log.i("@@@", "wayPoints size is " + wayPoints.size()); } } public class GPSLocation extends Service implements LocationListener { private Run run; private WayPoint wayPoint; private int distanceTravelled; @Override public void onCreate() { locationManager = (LocationManager)getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); startLocationUpdates = new ArrayList&lt;Location&gt;(); Log.i(LOGTAG, "GPSLocation Service Running..."); } public void startRunning() { setState(GPS_STATE_RUNNING); startTime = System.currentTimeMillis(); run = new Run(); run.setRoute(route); run.setDate(new Date()); } public void storeRun(long elapsedTime) { // if first run and end point not set then set end point run.setTime(elapsedTime); route.addToCumulativeDistance(distanceTravelled); DatabaseManager.getInstance().storeRun(run); DatabaseManager.getInstance().updateRoute(route); resetGlobalState(); } public void onLocationChanged(Location location) { previousLocation = currentLocation; currentLocation = location; Intent i = null; boolean startActivity = true; switch(state) { case GPS_STATE_SETTING_START_LOCATION: startLocationUpdates.add(location); if(startLocationAccepted()) { i = new Intent(this, AddRoute.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setAction("acquired"); } else { startActivity = false; // waiting for more location data } break; case GPS_STATE_READY: if(!atLocation(ROUTE_START, location, ROUTE_DELIMITER_REQUIRED_PROXIMITY)) { setState(GPS_STATE_AWAITING_ARRIVAL_AT_START_LOCATION); i = new Intent(this, Running.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setAction("proximity_update"); } break; case GPS_STATE_AWAITING_ARRIVAL_AT_START_LOCATION: i = new Intent(this, Running.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if(atLocation(ROUTE_START, location, ROUTE_DELIMITER_REQUIRED_PROXIMITY)) { setState(GPS_STATE_READY); i.setAction("ready"); } else { i.setAction("proximity_update"); } break; case GPS_STATE_RUNNING: createWayPoint(location); i = new Intent(this, Running.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if(atLocation(ROUTE_END, location, ROUTE_DELIMITER_REQUIRED_PROXIMITY) &amp;&amp; (distanceTravelled &gt; ROUTE_DELIMITER_REQUIRED_PROXIMITY)) { i.setAction("at_end"); } else { i.setAction("update"); } distanceTravelled += currentLocation.distanceTo(previousLocation); i.putExtra("distanceTravelled", distanceTravelled); setState(GPS_STATE_RUNNING_UPDATE); break; default: // error } } private void createWayPoint(Location location) { wayPoint = new WayPoint(); wayPoint.setLatitude(LocationMath.degreesToMicroDegrees(location.getLatitude())); wayPoint.setLongitude(LocationMath.degreesToMicroDegrees(location.getLongitude())); wayPoint.setRun(run); } /* * Now we have the elapsed time from the Running Activity, we can store this WayPoint */ public void updateWayPointWithElapsedTime(long elapsedTime) { // TODO::check the time value is correct wayPoint.setTimeOffset((int)elapsedTime); DatabaseManager.getInstance().storeWayPoint(wayPoint); setState(GPS_STATE_RUNNING); } } </code></pre> <p>Excerpts from DatabaseManager --></p> <pre><code>public void storeWayPoint(WayPoint point) { try { getHelper().getWayPointDao().create(point); } catch (SQLException e) { e.printStackTrace(); } } public void storeRun(Run run) { try { getHelper().getRunDao().create(run); } catch (SQLException e) { e.printStackTrace(); } } </code></pre>
 

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