我有一個模型ModelA
這是兩個RealmModules
的一部分。在一個RealmModule
我希望模型有一個主鍵和另一個我沒有。所以我試圖在Android應用程序啓動時從模型中刪除主鍵。RealmObjectSchema.removePrimaryKey和columnIndex小於0
我是否曾經提到,在一個模塊中,該模型即ModelA
充當父模型,而在其他角色中則是子模型的子模型即ModelC - > ModelB - > MODELA
以下是代碼片段:
public class ModelA extends RealmObject {
@PrimaryKey
private long id;
@Required
private String name;
//public getters and setters
}
而在其他RealmModule
我有ModelB和ModelC它們是這樣的:
public class ModelB extends RealmObject{
@PrimaryKey
private long id;
ModelA childModelA;
//public getters and setters
}
public class ModelC extends RealmObject{
@PrimaryKey
private long id;
ModelB childModelB;
//public getters and setters
}
個
而且模塊本身的樣子:
@RealmModule(classes = {
ModelA.class
} , library = true)
public class SmallModule{}
@RealmModule(classes = {
ModelA.class,
ModelB.class,
ModelC.class,
} , library = true)
public class BigModule{}
這就是我正在嘗試當模式/模塊BigModule
final RealmObjectSchema objectSchema = realmSchema.get(ModelA.class.getSimpleName());
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
objectSchema.removePrimaryKey();
objectSchema.addIndex("id");
});
到目前爲止好,除去從MODELA的PrimaryKey的。當我嘗試複製/更新到SmallModule
的領域,它工作得很好。但只要我嘗試在BigModule
做相同的,這是我所得到的:
04-10 21:49:17.002 12946-13321/org.jay.test1.debug E/REALM_JNI: jni: ThrowingException 2, columnIndex is less than 0., .
04-10 21:49:17.002 12946-13321/org.jay.test1.debug E/REALM_JNI: Exception has been throw: columnIndex is less than 0.
04-10 21:49:17.017 12946-12946/org.jay.test1.debug V/InputMethodManager: onWindowFocus: android.support.v4.widget.DrawerLayout{943b8b0 VFED..... .F....ID 0,0-480,800 #7f110086 app:id/drawer_layout} softInputMode=19 first=false flags=#81810100
04-10 21:49:17.017 12946-12946/org.jay.test1.debug V/InputMethodManager: START INPUT: android.support.v4.widget.DrawerLayout{943b8b0 VFED..... .F....ID 0,0-480,800 #7f110086 app:id/drawer_layout} ic=null [email protected] controlFlags=#101
04-10 21:49:17.043 12946-12946/org.jay.test1.debug E/DBHelper: [main::1] Error while inserting record
04-10 21:49:17.045 12946-12946/org.jay.test1.debug E/DBHelper: [main::1] Subscriber Error::columnIndex is less than 0.
java.lang.ArrayIndexOutOfBoundsException: columnIndex is less than 0.
at io.realm.internal.Table.nativeFindFirstInt(Native Method)
at io.realm.internal.Table.findFirstLong(Table.java:1077)
at io.realm.ModelARealmProxy.copyOrUpdate(ModelARealmProxy.java:965)
at io.realm.ModelBRealmProxy.copy(ModelBRealmProxy.java:810)
at io.realm.ModelBRealmProxy.copyOrUpdate(ModelBRealmProxy.java:779)
at io.realm.ModelCRealmProxy.copy(ModelCRealmProxy.java:1183)
at io.realm.ModelCRealmProxy.copyOrUpdate(ModelCRealmProxy.java:1136)
at io.realm.BigModuleMediator.copyOrUpdate(BigModuleMediator.java:370)
at io.realm.Realm.copyOrUpdate(Realm.java:1505)
at io.realm.Realm.copyToRealmOrUpdate(Realm.java:953)
at org.jay.test1.DBHelper$1$1.execute(DBHelper.java:66)
at io.realm.Realm$1.run(Realm.java:1402)
at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
境界是2.3.1
您絕對應該在這裏提交一個問題https://github.com/realm/realm-java/issues並提供一個再現此問題的最小示例 – EpicPandaForce
看起來您正在將模式更改爲遷移塊?如果你這樣做,肯定會出錯。由於所有列索引都在第一次打開類型化領域時被緩存,並且架構更改不會反映到類型化的Realm中。請問爲什麼你需要這樣做? – beeender
@beender有兩個模式,只有其中一個需要有主鍵。我需要從一個模式複製到另一個模塊來維護歷史。因此,如果存在Primarykey並且我執行insertOrUpdate,它會覆蓋之前的記錄,這會超過維護歷史記錄的目的。如果我嘗試插入它會引發異常。 是的,這是通過遷移塊完成的。在創建MainActivity時執行PrmaryKey刪除。 – Jay