简体   繁体   中英

Parse.com relations with ParseObject

I'm trying to fix relation between two ParseObjects: Place & Visit. I tried the method of extending the objects to ParseObjects which is a clean approach. The problem is the related Place object does not get saved.

Place:

@ParseClassName("Place")
public class Place extends ParseObject {

    public Place() {
    }

    private String title;
    private ParseGeoPoint geoPoint;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public ParseGeoPoint getGeoPoint() {
        return geoPoint;
    }

    public void setGeoPoint(ParseGeoPoint geoPoint) {
        this.geoPoint = geoPoint;
    }

    //Get distance to current location
    public double getDistance(ParseGeoPoint currentLocation) {
        return this.geoPoint.distanceInKilometersTo(currentLocation);
    }
}

Visit

@ParseClassName("Visit")
public class Visit extends ParseObject {

    public Visit() {
    }

    private long timestamp;
    private long duration;

    private Place place;

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public Place getPlace() {
        return place;
    }

    public void setPlace(Place place) {
        this.place = place;
    }
}

This is the way I save to the backend:

final Place place = new Place();
        place.setTitle("Home");

        final Visit visit = new Visit();
        visit.setTimestamp(Utils.getUnixNow());
        visit.setPlace(place);

        visit.saveEventually(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Logger.d("visit " + place.getTitle() + " was saved");
                } else {
                    Logger.d("Place was not saved, error " + e.getCode());
                }
            }
        });

Both classes are registered in the MyApplication class.

First of all: Your implementation of Parse classes is wrong.

1- remove all field variables

2- create the getters and setters as following

public String getTitle() {
    return get("Title"); // 'Title' is the column name in your Parse server table
}

public void setTitle(String title) {
    put("Title", title);
}

if you want to return a parse object,

public ParseObject  getPlace () { // it's ParseObject  not Place
    return get("Place "); // 'Place ' is the column name in your Parse server table
}

public void setPlace (ParseObject title) { // it's ParseObject  not Place 
    put("Place ", title);
}

Creating objects

Place place= ParseObject.create(Place .class);
place.setTitle("some title");

Visit visit= ParseObject.create(Visit.class);
visit.setPlace(place);
visit.save(); // or use saveInBackground();

In Your Application Class

    ParseObject.registerSubclass(Visit.class);
    ParseObject.registerSubclass(Place.class);
    Parse.initialize(this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM