我不明白为什么for循环会破坏我的应用程序。我知道这是一个简单的问题,但我已经盯着这段代码看了好几个小时试图找出答案。我非常确定build.gradle中的所有依赖项都是正确的。如果你需要更多的信息请告诉我。谢谢您。
//app类
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;
}
}
//building类
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;
}
}
//project 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'
}
最佳答案
我的答案很糟糕,但objectbox只适合“平面”数据结构。关系实现很糟糕,就像realmdb一样糟糕。为什么我们不能嵌入对象,为什么所有的东西都是关系?还有一个慢得多的选择,Couchbase Mobile。它几乎不被提及,但更像是移动版的MongoDB。我以前试过,不喜欢它-我仍然不喜欢它,但我不必花很多天试图找到文件,使它工作。只需保存数据结构。它不像objectbox那样类型安全(很糟糕)。似乎所有的产品都有不同程度的缺陷。
所以考虑一下:
objectbox对于平面结构和性能来说是最好的,但是与服务器响应的集成是很糟糕的(您必须为很多简单的事情编写大量的boiler plate,比如更新太多的关联-toone,并且文档对于kotlin和java都很糟糕)
realmdb有可怕的多线程问题,它比objectbox慢,但比couchbase mobile快得多。
couchbase移动性能很差,但当您从服务器接收到服务器结构时,可以存储它们。如果couchbase mobile修复了它们的查询/插入性能,那么当它根据需要存储对象结构时(例如,一个映射),它将粉碎其他的性能。
我知道这没用,投我一票,但我花了一段时间玩所有这些,仍然没有一个明确的路径离线存储为基础的应用程序。