结合Realm.io和Firebase

时间:2016-05-18 15:43:41

标签: java android firebase realm

我想创建可以在线和离线运行的Android应用程序。可以将Realm.io用于离线数据库,将Firebase用作在线服务。

我试图创建直接将realm对象添加到firebase的简单应用程序,但我得到的错误异常如下:

java.lang.NoClassDefFoundError: rx.Observable
at libcore.reflect.InternalNames.getClass(InternalNames.java:55)
at java.lang.Class.getDexCacheType(Class.java:476)
at java.lang.reflect.Method.getReturnType(Method.java:183)
at java.lang.Class.getDeclaredMethods(Class.java:672)
at com.fasterxml.jackson.databind.introspect.AnnotatedClass._addMemberMethods(AnnotatedClass.java:593)
at com.fasterxml.jackson.databind.introspect.AnnotatedClass.resolveMemberMethods(AnnotatedClass.java:421)
at com.fasterxml.jackson.databind.introspect.AnnotatedClass.memberMethods(AnnotatedClass.java:243)
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._addMethods(POJOPropertiesCollector.java:457)
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.collect(POJOPropertiesCollector.java:233)
at com.fasterxml.jackson.databind.introspect.BasicClassIntrospector.collectProperties(BasicClassIntrospector.java:142)
at com.fasterxml.jackson.databind.introspect.BasicClassIntrospector.forSerialization(BasicClassIntrospector.java:68)
at com.fasterxml.jackson.databind.introspect.BasicClassIntrospector.forSerialization(BasicClassIntrospector.java:11)
at com.fasterxml.jackson.databind.SerializationConfig.introspect(SerializationConfig.java:490)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:133)
at com.fasterxml.jackson.databind.SerializerProvider._createUntypedSerializer(SerializerProvider.java:873)
at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:833)
at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:387)
at com.fasterxml.jackson.databind.SerializerProvider.findTypedValueSerializer(SerializerProvider.java:478)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:97)
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2593)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.firebase.client.Firebase.setValueInternal(Firebase.java:336)
at com.firebase.client.Firebase.setValue(Firebase.java:298)
at com.pringstudio.firebaserealm.MainActivity$1.onClick(MainActivity.java:56)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                Caused by: java.lang.ClassNotFoundException: Didn't find class "rx.Observable" on path: DexPathList[[dex file "/data/data/com.pringstudio.firebaserealm/files/instant-run/dex/slice-tubesock-0.0.12_19946b0113470caa2e5e499f498247824b9e76d9-classes.dex", dex file "/data/data/com.pringstudio.firebaserealm/files/instant-run/dex/slice-support-annotations-23.3.0_ce4b168a6a58bc3fcd41c2a9e083ac97b17599a0-classes.dex", dex file "/data/data/com.pringstudio.firebaserealm/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.pringstudio.firebaserealm/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.pringstudio.firebaserealm/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.pringstudio.firebaserealm/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.pringstudio.firebaserealm/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.pringstudio.firebaserealm/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.pringstudio.firebaserealm/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.pringstudio.

代码

// Firebase Root
Firebase firebase = new Firebase("firebae_url");

Siswa siswa = new Siswa(
 Integer.valueOf(editText.getText().toString()),
 editTextNama.getText().toString()
);

Firebase refSiswa = firebase.child("siswa").child(String.valueOf(siswa.getId()));
refSiswa.setValue(siswa, new Firebase.CompletionListener() {
 @Override
 public void onComplete(FirebaseError firebaseError, Firebase firebase) {
  if (firebaseError != null) {
   Log.e("Firebase Add", "Error: " + firebaseError.getMessage());
   Toast.makeText(getApplicationContext(), "Gagal\n" + firebaseError.getMessage(), Toast.LENGTH_SHORT).show();
  } else {
   Toast.makeText(getApplicationContext(), "Data tersimpan", Toast.LENGTH_SHORT).show();
  }
 }
});

对象

package com.pringstudio.firebaserealm.model;

import io.realm.RealmObject;

/**
 * Created by sucipto on 5/18/16.
 */
public class Siswa extends RealmObject{

    private String nama;
    private int id;

    public Siswa(){};

    public Siswa(int id, String nama){
        this.id = id;
        this.nama = nama;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }
}

当我没有扩展RealmObject时,firebase可以接受它,但是当我将Siswa Object转换为扩展领域对象时,它会导致错误。

我希望我的对象与firebase和领域兼容,任何消化?感谢。

3 个答案:

答案 0 :(得分:9)

看起来你正在使用Jackson Databind,这就是导致问题的原因。这里有一个问题的描述和解决方法: https://realm.io/docs/java/latest/#jackson-databind

但是也有答案:

这可以通过将RxJava添加到项目中来修复,也可以创建一个如下所示的空虚拟文件。

package rx;

public class Observable {
    // Dummy class required for Jackson-Databind support if
    // RxJava is not a project dependency.
}

答案 1 :(得分:0)

在我的情况下,RealmObject试图更新Firebase,但我收到一个错误,提到Firebase无法处理序列化对象。我所做的是克隆RealmObject,这样就可以从Realm更新到Firebase。

我使用Realm启动了一个应用程序,但意识到需要同步数据。所以这是一个快速的解决方案

public void syncWithFirebase(){

DatabaseReference addresses = mRef.getDB().child(TESTABLE_ADDRESSES);
long numOfAddresses = mProvider.countAddresses();
ShortAddress address;

for( int i = 1; i <= numOfAddresses; i++ ){
    address = mProvider.getAddress( i );

    //we get a serialized error, so right now address is copied
    addresses
        .child( Long.toString(address.getAddressId()) )
        .setValue(AddressUtils.cloneAddress(address));
}

}

public void syncWithRealm( Response<Boolean> response){
DatabaseReference addresses = mRef.getDB().child(TESTABLE_ADDRESSES);


addresses.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for( DataSnapshot snapshot: dataSnapshot.getChildren() ){
            ShortAddress address = snapshot.getValue(ShortAddress.class );
            mProvider.updateAddress( address );
        }

        response.onResult(true);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        response.onError( new Exception( databaseError.getMessage() ) );
    }
});

}

答案 2 :(得分:0)

java.lang.NoClassDefFoundError: rx.Observable

如果您在使用 Firebase 的java / android项目中看到此异常 - 为您提供简单的解决方案:

  

导入firebase的类必须是免费的(不要从中扩展)   其他课程。)

我在我的android项目中使用 Realm Firebase - 对于两个基础上的加载数据,我创建了两个相同的类。

对于Realm来说

public class Todo extends RealmObject {/*fields*/}

for Firebase:

public class TodoDTO {/*fields*/}

从Todo复制到TodoDTO并返回。