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

标签 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;
    }
}

//项目构建.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 那样类型安全(很糟糕)。似乎所有产品在不同层面上都很糟糕。

考虑一下:

  1. ObjectBox 是平面结构和性能的最佳选择,但与服务器响应的集成很糟糕(您必须为简单的事情编写太多样板,例如更新 TooMany 关联 - ToOne,并且文档很糟糕Kotlin 和 Java 相似)

  2. RealmDb 存在严重的多线程问题,比 ObjectBox 慢,但比 Couchbase Mobile 快得多。

  3. Couchbase Mobile 的性能很糟糕,但您可以在从服务器接收到服务器结构时存储它们。如果 Couchbase Mobile 修复了它们的查询/插入性能,它会粉碎其他的,因为它会根据需要存储您的对象结构(例如 MAP)

我知道这无济于事,请否决我,但我花了一段时间尝试所有这些,但仍然没有找到基于离线存储的应用程序的明确路径。

关于Android ObjectBox.getAll() 返回 null,即使 objectBox.count 为 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53567196/

相关文章:

android 声音应用程序 sdk 或 ndk?

android - 我的应用程序在哪里?/数据显示为空

android - 如何获取捕获的图像文件路径并保存到数据库?

android - 如何从一个应用程序快速集成多个商家

android - 如何从android中的BoxStore Objectbox数据库中获取不同的值

android - 如何在对话框中制作编辑文本框

ios - "fb:explicitly_shared"和 "no_feed_story"参数有什么区别?

java - ObjectBox:获取具有特定关系的对象

java - 将自定义类型的列表转换为对象框中的字符串

javascript - $.mobile.changePage 不加载 CSS