使用ORMLite迭代外部集合的问题

时间:2012-05-07 01:12:26

标签: ormlite

尝试使用以下类的Ormlite ForeignCollection时遇到问题。我相信我有我的Run&amp; Waypoint类正确注释。 (Run有一个WayPoints集合)。但是,当我尝试访问WayPoints时(请参阅第三个代码提取),当我调用CloseableIterator时,hasNext()返回null。当我使用模拟器测试时,我首先为Run生成一些WayPoints并存储它们。然后我尝试通过Run实例访问这些WayPoints。只是为了完成图片而不在这里转储太多代码,ForeignCollection<WayPoint>对象通过调用

传递给Contender构造函数
run.getWayPoints();

这里是我如何创建一个WayPoint并存储它:: - 当GPSLocation.onLocationChanged()触发并且状态为GPS_STATE_RUNNING时,则调用createWayPoint()。这将实例化WayPoint,设置lng / lat值,并设置相关Run实例的句柄,此时此实例尚未将其自身存储到db。 Intent被发送到另一个类,该类在GPSLocation.updateWayPointWithElapsedTime()上回调,其中WayPoint最终存储到db。当运行完成时,外部类调用storeRun()来存储Run实例。我开始想知道当WayPoint的相关Run实例本身尚未存储时是否存储WayPoint是问题的一部分。

**注意::这些类不会被编译,因为它们被缩减以显示与问题相关的部分。

@DatabaseTable
public class Run {

    @DatabaseField(generatedId=true)
    private int id;

    @DatabaseField(foreign=true,foreignAutoRefresh=true)
    private Route route;

    @ForeignCollectionField
    private ForeignCollection<WayPoint> wayPoints; 

    @DatabaseField
    private Date date;

    @DatabaseField
    private long time;

    public ForeignCollection<WayPoint> 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<Contender> {

    private int accumulatedDistance;
    private int interpolatedDistance;
    private WayPoint startPoint;
    CloseableIterator itr;
    private int id;
    WayPoint previous;
    WayPoint next;

    public Contender(ForeignCollection<WayPoint> 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<Location>();

            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) 
                            && (distanceTravelled > 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); 
        }
    }

摘自DatabaseManager - &gt;

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();
    }
}

1 个答案:

答案 0 :(得分:1)

我发现你发布Chris的代码没有任何问题。因此我怀疑你没有像你认为的那样插入WayPoint。你确认他们在数据库中吗?您是否直接使用WayPoint DAO尝试了查询?

// what does this return?
List<WayPoint> wayPoints = wayPointDao.queryForAll();
  

我首先为Run生成一些WayPoints并存储它们

此外,请确保您已将Run字段插入Run表并在每个WayPoint 上设置它,然后为{创建{ {1}}。你应该做的事情如下:

WayPoint

您可以插入的另一种方法是使用外部集合本身:

Run run1 = new Run(...);
runDao.create(run1);
...
WayPoint wayPoint1 = new WayPoint(...);
wayPoint1.setRun(run1);
wayPointDao.create(wayPoint1);
...

如果您编辑问题并向我们展示用于将// create a foreign collection we can add to run.wayPoints = runDao.getEmptyForeignCollection("wayPoints"); // now if you add something to the collection, it gets created in the DAO as well run.wayPoints.add(wayPoint1); 插入数据库的代码,我将编辑我的答案以提供更多信息。