I do not understand why the for loop is breaking my application. I understand that this is a simple question, but I have been staring at this code for hours attempting to figure it out. I am fairly certain all of the dependencies in the build.gradle are correct. Let me know if you need more information. Thank you.
// App Class
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 Class
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'
}
My answer is a terrible answer, but ObjectBox is only good for "flat" data structures. The relations implementation is horrible, as bad as RealmDb's. Why can't we embed objects, why is everything a relation? There's a much slower alternative, Couchbase Mobile. It hardly gets a mention but it's more like MongoDb for mobile. I tried it before, didn't like it - I still don't like it but I don't have to spend days and days trying to find documentation to make it work. You just save your data structure. It's not typesafe like ObjectBox (which sucks). It seems all offerings suck at different levels.
So consider this:
ObjectBox is the best for flat structures and performance, but sucks integrating with server responses ( you have to write TOOOO much boiler plate for simple things such as updates of TooMany associations - ToOne as well, and the documentation sucks for Kotlin and Java alike)
RealmDb has terrible multi-threading issues, and is slower than ObjectBox, but much faster than Couchbase Mobile.
Couchbase Mobile performance sucks, but you can store your server structures as you receive them from server. If Couchbase Mobile fixed their query/insert performance, it'll smash the others as it stores your object structures as you need it (e.g. a MAP)
I know this doesn't help, vote me down, but I spent a while playing with all of these and still don't have a clear path for offline storage based apps.
Turns out that when using ObjectBox in android development it is crucial to have an empty constructor. This may relate to your answer is a sense, but that is what fixed the program. Very dumb implementation in my opinion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With