RealmException:主键约束已损坏。值已经存在:1

时间:2019-05-09 11:14:00

标签: android realm

当应用程序尝试在领域“ io.realm.exceptions.RealmException:主键约束已损坏。值已存在:1”错误出现时,我在Realm的logcat中收到新错误< / p>

我已经在互联网上进行搜索,但是我发现的所有关于Value的答案已经存在:0。 我尝试过不同类型的调用,例如在createAllFromJson()之前插入realm.clear()或将createAllFromJson更改为createOrUpdateAllFromJson,但它不起作用。 你们中有人知道数字1指的是什么吗?

这是代码->

if (null != realm) {
            try {
                realm.beginTransaction();
                //realm.clear(StationRealm.class);
                realm.createAllFromJson(StationRealm.class, json);
                //realm.createOrUpdateAllFromJson(StationRealm.class, json);
                realm.commitTransaction();
                sendStatusRefreshedMessage(context);
            } catch (Exception ex) {
                realm.cancelTransaction();
                Log.e(TAG, "RealmDBStations: " + ex.getMessage());
            }
        }

StationRealm.class->

@Parcel(value = Parcel.Serialization.BEAN, analyze = { StationRealm.class })
@RealmClass
public class StationRealm extends RealmObject {
    @PrimaryKey
    private String id;
    private String name;
    private String address;
    private String addressNumber;
    private String zipCode;
    private String districtCode;
    private String districtName;
    private double altitude;
    private String stationType;
    private LocationRealm location;
    private int bikes;
    private int slots;
    private double distanceTo;

    public StationRealm() {};

    public StationRealm(StationRealm station) {
        this.id = station.getId();
        this.name = station.getName();
        this.address = station.getAddress();
        this.addressNumber = station.getAddressNumber();
        this.zipCode = station.getZipCode();
        this.districtCode = station.getDistrictCode();
        this.districtName = station.getDistrictName();
        this.altitude = station.getAltitude();
        this.stationType = station.getStationType();
        this.location = station.getLocation();
        this.bikes = station.getBikes();
        this.slots = station.getSlots();
        this.distanceTo = station.getDistanceTo();
    }
// getters and setters
}

Json->

[{"id":1,"name":"1 - Allen Street","address":"XXX","addressNumber":"0","zipCode":"11","districtCode":"1","districtName":"","altitude":null,"nearbyStations":[2,3,69],"location":{"lat":41.674,"lon":-0.967},"stationType":"BIKE"},{"id":2,"name":"2 - XXX","address":"XXX","addressNumber":"0","zipCode":"11","districtCode":"1","districtName":"","altitude":null,"nearbyStations":[1,3,69],"location":{"lat":41.66,"lon":-0.86},"stationType":"BIKE"}]

我尝试了一些新案例: 如果我插入这样的json->

[{"id":1,"name":"1 - Allen Street","address":"XXX","addressNumber":"0","zipCode":"11","districtCode":"1","districtName":"","altitude":null,"nearbyStations":[2,3,69],"location":{"lat":41.674,"lon":-0.967},"stationType":"BIKE"}]

错误是相同的,但是如果我将id 1更改为id 2,该错误将更改并显示io.realm.exceptions.RealmException:主键约束已损坏。值已经存在:2。

领域干净如何可以存在另一个id:2?

0 个答案:

没有答案