首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android ObjectBox.getAll()返回null,尽管objectBox.count为5

Android ObjectBox.getAll()返回null,尽管objectBox.count为5
EN

Stack Overflow用户
提问于 2018-12-01 02:13:09
回答 2查看 1.1K关注 0票数 3

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

//应用程序类

代码语言:javascript
复制
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;
    }
}

//建筑班

代码语言:javascript
复制
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项目

代码语言:javascript
复制
// 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

代码语言:javascript
复制
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'
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-11 21:53:31

事实证明,在安卓开发中使用ObjectBox时,有一个空的构造函数是至关重要的。这可能与你的回答有关是一种感觉,但这就是修正程序的原因。在我看来是很愚蠢的执行。

票数 2
EN

Stack Overflow用户

发布于 2019-02-09 08:32:47

我的回答是一个糟糕的答案,但是ObjectBox只对“平面”数据结构有好处。关系实现是可怕的,和RealmDb一样糟糕。为什么我们不能嵌入对象,为什么一切都是关系?还有一种更慢的选择,Couchbase Mobile。几乎没有人提到它,但它更像是手机上的MongoDb。我以前试过,不喜欢它-我仍然不喜欢它,但我不需要花费数天的时间去寻找文件来使它工作。你只要保存你的数据结构。它不像ObjectBox那样的打字(这很糟糕)。似乎所有的产品在不同的层次上都很糟糕。

因此,请考虑一下:

  1. ObjectBox在平面结构和性能方面是最好的,但是与服务器响应的集成很糟糕(您必须为简单的事情编写大量的锅炉板,例如TooMany关联的更新- ToOne,以及文档对Kotlin和ToOne都很糟糕)。
  2. RealmDb存在可怕的多线程问题,比ObjectBox慢,但比Couchbase Mobile快得多。
  3. Couchbase Mobile性能糟糕透顶,但是您可以在从服务器接收到它们时存储服务器结构。如果Couchbase Mobile修复了他们的查询/插入性能,它会在存储您需要的对象结构(例如地图)时粉碎其他的查询/插入性能。

我知道这没有帮助,投我反对票,但我花了一段时间玩所有这些,仍然没有一个明确的路径离线存储为基础的应用程序。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53567196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档