Android ObjectBox.getAll() 返回 null,即使 objectBox.count 是 5

我不明白为什么 for 循环会破坏我的应用程序。我知道这是一个简单的问题,但我一直盯着这段代码几个小时试图弄清楚。我相当确定 build.gradle 中的所有依赖项都是正确的。如果您需要更多信息,请与我们联系。谢谢你。

//应用类

package edu.uncw.seahawktours;

import android.app.Application;

import java.util.ArrayList;
import java.util.List;

import io.objectbox.Box;
import io.objectbox.BoxStore;

public class App extends Application {

private BoxStore boxStore;

@Override
public void onCreate() {
    super.onCreate();

    // Initialize the main data access object
    boxStore = MyObjectBox.builder().androidContext(App.this).build();

    // Get the wrapper (Box) for the Book table that lets us store Book objects
    Box<Building> buildingBox = boxStore.boxFor(Building.class);

    // Initialize with some data
    if (buildingBox.count() == 0) {
        List<Building> initialBooks = new ArrayList<>();
        initialBooks.add(new Building("CIS", "https://library.uncw.edu/web/collections/archives/bnl/cis.html", R.drawable.cis));
        initialBooks.add(new Building("Depaolo Hall", "https://library.uncw.edu/web/collections/archives/bnl/6.html", R.drawable.depaolo));
        initialBooks.add(new Building("Trask Coliseum", "https://library.uncw.edu/web/collections/archives/bnl/10.html", R.drawable.trask));
        initialBooks.add(new Building("King Hall", "https://library.uncw.edu/web/collections/archives/bnl/4.html", R.drawable.kinghall2));
        initialBooks.add(new Building("Leutze Hall", "https://library.uncw.edu/web/collections/archives/bnl/17.html", R.drawable.leutzehall));

        // ObjectBox is smart enough to handle CRUD on Collections of entities
        buildingBox.put(initialBooks);
    }

    System.out.println(buildingBox.count());

    for (Building book : buildingBox.getAll()) {
        System.out.println(book.getBuildingName());
    }

}

public BoxStore getBoxStore() {
    return boxStore;
    }
}

//建筑类
package edu.uncw.seahawktours;

import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;

@Entity
public class Building {
    @Id public long id;
    private String buildingName;
    private String description;
    private String url;
    private int imageID;


public Building(String name, String url ,int buildingPictureID){
    this.buildingName = name;
    this.url = url;
    this.imageID = buildingPictureID;
}

public long getId() {
    return id;
}

public String getDescription() {
    return description;
}

public String getBuildingName() {
    return buildingName;
}

public String getUrl() {
    return url;
}

public int getImageID() {
    return imageID;
}

public void setId(long id) {
    this.id = id;
}

public void setBuildingName(String buildingName) {
    this.buildingName = buildingName;
}

public void setDescription(String description) {
    this.description = description;
}

public void setUrl(String url) {
    this.url = url;
}

public void setImageID(int imageID) {
    this.imageID = imageID;
}

@Override
public String toString() {
    return this.buildingName;
    }
}

//项目 Build.Gradle
// Top-level build file where you can add configuration options common to 
all sub-projects/modules.

buildscript {
    ext.objectboxVersion = '2.2.0'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"


    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

//应用程序构建.Gradle
apply plugin: 'com.android.application'
apply plugin: 'io.objectbox'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "edu.uncw.seahawktours"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
}

最佳答案

我的答案是一个糟糕的答案,但 ObjectBox 只适用于“平面”数据结构。关系实现很糟糕,和 RealmDb 一样糟糕。为什么我们不能嵌入对象,为什么一切都是关系?有一个更慢的替代方案,Couchbase Mobile。它几乎没有被提及,但它更像是移动版的 MongoDb。我之前尝试过,不喜欢它 - 我仍然不喜欢它,但我不必花费数天时间来寻找文档以使其工作。您只需保存您的数据结构。它不像 ObjectBox 那样类型安全(这很糟糕)。似乎所有的产品都在不同的层面上很糟糕。

所以考虑一下:

  • ObjectBox 是平面结构和性能的最佳选择,但与服务器响应的集成很糟糕(您必须为简单的事情编写太多的样板,例如更新太多关联 - ToOne,并且文档对于 Kotlin 和 Java 来说都很糟糕)
  • RealmDb 有严重的多线程问题,比 ObjectBox 慢,但比 Couchbase Mobile 快得多。
  • Couchbase Mobile 性能很差,但是您可以在从服务器接收服务器结构时存储它们。如果 Couchbase Mobile 修复了它们的查询/插入性能,它将粉碎其他的,因为它会根据您的需要存储您的对象结构(例如 MAP)

  • 我知道这无济于事,投票给我,但我花了一段时间玩所有这些,仍然没有基于离线存储的应用程序的明确路径。