【问题标题】:Objectbox Another BoxStore is still open In Android MVVM Using DaggerObjectbox 另一个 BoxStore 仍然在使用 Dagger 在 Android MVVM 中打开
【发布时间】:2023-03-09 23:05:01
【问题描述】:

我在 Android MVVM 项目中使用 DaggerObjectBox

我尝试提供 BoxStore 的单例以及表 Rfid、体重、健康 和我的 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);
    }
}

【问题讨论】:

  • 欢迎来到 Dagger。我建议在创建 BoxStore 时设置断点并查看日志消息以了解其他 ObjectBox 创建。

标签: android mvvm objectbox dagger


【解决方案1】:

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

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()));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    相关资源
    最近更新 更多