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

问题描述 投票:0回答:1

我在Android MVVM项目中使用Dagger和ObjectBox

我试图提供一个Singleton of BoxStore,还有Tables Rfid,Weight,Health和我的AppDBH类

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

引起:io.objectbox.exception.DbException:此目录仍然打开另一个BoxStore:/data/data/com.sarveen.framework.forceplate/files/objectbox/objectbox。提示:对于大多数应用程序,建议在应用程序的生命周期内保留BoxStore

这是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);
    }
}
android mvvm objectbox dagger
1个回答
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()));
}
© www.soinside.com 2019 - 2024. All rights reserved.