Objectbox另一个BoxStore仍在Android MVVM中使用Dagger打开

时间:2019-03-02 08:52:32

标签: android mvvm objectbox dagger

我在Android MVVM项目中使用 Dagger 一个 ObjectBox

我试图提供一个 BoxStore 以及表 Rfid,Weight,Health 和我的 AppDBH

当我将 AppDBH 添加到 AppModule 并尝试注入时,应用程序崩溃。

  

原因:io.objectbox.exception.DbException:此目录仍在打开另一个BoxStore:/data/data/com.sarveen.framework.forceplate/files/objectbox/objectbox。提示:对于大多数应用程序,建议在其使用寿命内保留BoxStore。3

这是 AppModule 类:

@Module
public class AppModule {

    @Provides
    @Singleton
    AppDBH provideAppDBH(Box<Rfid> rfidBox, Box<Weight> weightBox, Box<Health> healthBox) {
        return new AppDBH(rfidBox, weightBox, healthBox);
    }

    @Provides
    @Singleton
    Box<Rfid> provideBoxRfid(Context context) {
        return provideBoxStore(context).boxFor(Rfid.class);
    }

    @Provides
    @Singleton
    Box<Health> provideBoxHealth(Context context) {
        return provideBoxStore(context).boxFor(Health.class);
    }

    @Provides
    @Singleton
    Box<Weight> provideBoxWeight(Context context) {
        return provideBoxStore(context).boxFor(Weight.class);
    }

    @Provides
    @Singleton
    BoxStore provideBoxStore(Context context) {
        return MyObjectBox.builder()
                .androidContext(context)
                .build();
    }
}

这是 AppDBH 类:

@Singleton
public class AppDBH implements DBH{

    public AppDBH(Box<Rfid> rfidBox, Box<Weight> weightBox, Box<Health> healthBox) {
        this.rfidBox = rfidBox;
        this.weightBox = weightBox;
        this.healthBox = healthBox;
    }

    private final String TAG = "BOX_TAG";

    private Box<Rfid> rfidBox;

    private Box<Weight> weightBox;

    private Box<Health> healthBox;

    @Override
    public List<Rfid> getAllRfids() {
        return rfidBox.getAll();
    }

    @Override
    public List<Weight> getAllWeights() {
        return weightBox.getAll();
    }

    @Override
    public List<Health> getAllHealths() {
        return healthBox.getAll();
    }

    @Override
    public long insertRfid(Rfid rfid) {
        return rfidBox.put(rfid);
    }

    @Override
    public long insertWeight(Weight weight) {
        return weightBox.put(weight);
    }

    @Override
    public long insertHealth(Health health) {
        return healthBox.put(health);
    }
}

1 个答案:

答案 0 :(得分:0)

下班后我解决了这个问题:

  

DBH重命名为DbRepo

public class DbRepo implements DbRepository {

    private final String TAG = "BOX_TAG";
    private Box<Rfid> rfidBox;
    private Box<Weight> weightBox;
    private Box<Health> healthBox;

    public DbRepo(@NonNull BoxStore boxStore) {
        this.rfidBox = boxStore.boxFor(Rfid.class);
        this.weightBox = boxStore.boxFor(Weight.class);
        this.healthBox = boxStore.boxFor(Health.class);
    }
    // ...
}

@Module
public class AppModule {

    @Provides
    @Singleton
    DbRepo provideDbRepo(BoxStore boxStore) {
        return new DbRepo(boxStore);
    }

    @Provides
    @Singleton
    BoxStore provideBoxStore(Context context) {
        BoxStore boxStore = MyObjectBox.builder()
                .androidContext(context)
                .build();
        if (BuildConfig.DEBUG) {
            new AndroidObjectBrowser(boxStore).start(context);
        }
        return boxStore;
    }
    // ...
}

//测试代码

@Inject
DbRepo dbRepo;

void start() {
    final String BTG = "BOX_TAG";
    // dbRepo.setRfidBox(rfidBox);
    Log.e(TAG, BTG + ", rfidBox: " + (dbRepo != null) + " , " + (System.currentTimeMillis() - 1551513800000L));
    Rfid rfid = new Rfid();
    rfid.setAnimalRfid(System.currentTimeMillis() - 1551513800000L);
    rfid.setCountryCode(98);
    rfid.setTimestamp(System.currentTimeMillis());
    long id = dbRepo.insertRfid(rfid);
    Log.e(TAG, BTG + ", rfidBox id: " + (id));
    Log.e(TAG, BTG + ", rfidBox size: " + (dbRepo.getAllRfids().size()));
}