关于 Android with RxAndroid、Dagger 2 和 Realm 的问题

Questions about Android with RxAndroid, Dagger 2 and Realm

我目前正在开发一个应用程序,并希望为 Android 使用当前最先进的库:RxAndroid、Dagger2 和 Realm。我在网上找不到好的例子,所以我只好在这里问一下。

1。为什么我应该使用 Observable 作为 Realm 查询的 return 类型?

public Observable<Bla> getBla() {
    Bla b = realm.where(Bla.class).findFirst();
    return Observable.just(b);
}

在大多数情况下,Realm 在 UIThread 上查询 运行,所以为什么我不应该 return 结果作为列表而不是

Observable<List<Bla>>

也许有人知道这个 lib 组合中 realm 的一个很好的例子?

2。为什么我能够在模块中不提及提供程序的情况下注入服务。我的构造有问题吗?

@Inject
TableService mTableService;

(代码如下)

3。 ServiceComponent需要AppComponent的Application,如何正确配置@Component?

@Singleton
@Component(modules = {AppModule.class, ServiceModule.class})
public interface AppComponent {

    void inject(MainActivity activity);
}

@Singleton
@Component(modules = {AppModule.class, ServiceModule.class})
public interface ServiceComponent {

    void inject(MainActivity activity);
}

public class AppModule {

    Application mApplication;

    public AppModule(Application application) {
        mApplication = application;
    }

    @Provides
    @Singleton
    Application providesApplication() {
        return mApplication;
    }

    @Provides
    @Singleton
    RealmConfiguration provideRealmConfiguration(Application application) {
        return new RealmConfiguration.Builder(application)
               .schemaVersion(1)
               .deleteRealmIfMigrationNeeded()
               .build();
    }

    @Provides
    Realm provideRealm(RealmConfiguration realmConfiguration) {
        try {
            return Realm.getInstance(realmConfiguration);
        } catch (Exception cause) {
            Realm.deleteRealm(realmConfiguration);
            return Realm.getInstance(realmConfiguration);
        }
    }
}

@Module
public class ServiceModule {

    public ServiceModule() {
    }

    @Provides
    @Singleton
    GuestService provideGuestService(Realm realm) {
        return new GuestService(realm);
    }
}

public class Application extends android.app.Application {

    private AppComponent mAppComponent;

    @Override
    public void onCreate() {
        super.onCreate();

        mAppComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();

    }

    public AppComponent getAppComponent() {
        return mAppComponent;
    }
}

谢谢!

1.) RealmResults<T> 是 auto-updating,如果您将 RealmChangeListener 添加到 RealmResults,那么只要基础数据发生变化,就会调用它。

private final RealmChangeListener listener = new RealmChangeListener() {
    @Override
    public void onChange(Object results) {
        notifyDataSetChanged();
    }
};

但是当您在 RealmResults 上调用 asObservable() 时,会创建一个 Observable<RealmResults<T>>,它会自动附加一个 RealmChangeListener 来通知您更改,并在你退订。如果您检查默认的 RealmObservableFactory 实现,您可以查看确切的行为。

所以它确实是一个 shorthand,可以在您的结果中添加 RealmChangeListener,这样您就可以随时更新您的观点。

Subscription subscription = RxTextView.textChanges(editText).switchMap(charSequence -> 
    realm.where(SomeObject.class)
         .contains("searchField", charSequence.toString(), Case.INSENSITIVE)
         .findAllAsync()
         .asObservable())
.filter(RealmResults::isLoaded) //
.subscribe(objects -> adapter.updateData(objects));

2.) 您可能已经指定了一个 @Inject 带注释的构造函数。

public class TableService {
    @Inject
    Application application;

    @Inject
    public TableService() {
    }
}

3.)

编辑:

  1. Why should I use Observable as return type for Realm queries?
public Observable<Bla> getBla() {
    Bla b = realm.where(Bla.class).findFirst();
    return Observable.just(b);
}

这与 Realm 一起使用没有实际意义,因为它不监听更改。

用起来更合理

public Observable<Blah> getBla() {
    Blah blah = realm.where(Blah.class).findFirst();
    if(blah == null) {
        return Observable.empty();
    } else {
        return blah.asObservable();
    }
}

尽管personally I advise to use Observable<RealmResults<T>> because it is more reliable.

3.) Not sure what you mean. Service Component is not working without @component appModule.class, but it should be in my understanding.

甚至不应该一个ServiceComponent。就 @Component SingletonComponent.