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

时间:2018-12-01 02:13:10

标签: android mobile objectbox

我不明白为什么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
}

// App Build.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'
}

2 个答案:

答案 0 :(得分:1)

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

所以考虑一下:

  1. ObjectBox是最适合平面结构和性能的对象,但是它与服务器响应集成很糟(您必须为很多简单的事情(例如,TooMany关联的更新-ToOne)编写太多的样板,文档也很糟糕Kotlin和Java都一样)

  2. RealmDb具有可怕的多线程问题,并且比ObjectBox慢,但比Couchbase Mobile快得多。

  3. Couchbase移动性能很差,但是当您从服务器接收到服务器结构时,您可以存储它们。如果Couchbase Mobile修复了他们的查询/插入性能,它将在存储您需要的对象结构(例如MAP)时粉碎其他对象

我知道这无济于事,请我拒绝,但是我花了一些时间来处理所有这些问题,但是对于基于离线存储的应用程序仍然没有明确的路径。

答案 1 :(得分:0)

事实证明,在Android开发中使用ObjectBox时,拥有一个空的构造函数至关重要。这可能与您的答案是有道理的,但这就是修复程序的原因。我认为执行起来很愚蠢。