【发布时间】:2021-10-15 11:44:08
【问题描述】:
我正在尝试使用领域同步,我按照文档中的步骤操作,创建了一个模式,然后复制了领域在 android 应用程序中建议的 java 对象模型,但是对于某些属性,如 int,加倍它会给出错误.
架构:
{
"title": "Invoice",
"properties": {
"_id": {
"bsonType": "objectId"
},
"ownerId": {
"bsonType": "string"
},
"customerId": {
"bsonType": "string"
},
"date": {
"bsonType": "date"
},
"invoiceItems": {
"bsonType": "array",
"items": {
"bsonType": "object",
"properties": {
"_id": {
"bsonType": "objectId"
},
"quantity": {
"bsonType": "int"
},
"trackId": {
"bsonType": "string"
},
"unitPrice": {
"bsonType": "double"
}
}
}
},
"total": {
"bsonType": "double"
}
}
}
案例 1:如果我使用 int 和 double
public class Invoice extends RealmObject {
@PrimaryKey
private ObjectId _id;
private String ownerId;
private String customerId;
private Date date;
private double total;
RealmList<InvoiceItem> invoiceItems;
//getters, setters
}
错误:Failed to transform received changeset: Schema mismatch: Property 'total' in class 'Invoice' is nullable on one side and not on the other.
案例 2:如果我使用 Integer 和 Double
public class Invoice extends RealmObject {
@PrimaryKey
private ObjectId _id;
private String ownerId;
private String customerId;
private Date date;
private Double total;
RealmList<InvoiceItem> invoiceItems;
//getters, setters
}
错误:E/AndroidRuntime: FATAL EXCEPTION: main Process: realm.example.mediastore, PID: 12939 java.lang.IllegalStateException: The following changes cannot be made in additive-only schema mode: - Property 'Invoice.total' has been made optional. at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method) at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:175) at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:251) at io.realm.BaseRealm.<init>(BaseRealm.java:141) at io.realm.BaseRealm.<init>(BaseRealm.java:108) at io.realm.Realm.<init>(Realm.java:159) at io.realm.Realm.createInstance(Realm.java:495) at io.realm.RealmCache.createInstance(RealmCache.java:494) at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:461) at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:422) at io.realm.Realm.getInstance(Realm.java:424) ...
所以两者都不起作用。我不知道如何解决这个问题。
【问题讨论】:
-
错误很明显 *在仅添加模式模式下无法进行以下更改:- 属性“Invoice.total”已设为可选。 - 这意味着什么是因为您正在同步,所以只允许添加更改。如果您将属性从非可选更改为可选(如错误所示),这是一个破坏性更改,需要擦除客户端并与服务器重新同步。这也与您的prior question 非常相似
-
在我之前的问题中,我没有使用领域生成的对象模型,而是尝试自己构建它,而所需的属性就是问题所在。虽然在这里我复制了确切的模型,但仍然出错。不过谢谢,我会清除客户端。
标签: android synchronization realm realm-mobile-platform object-model