【问题标题】:Questions about Android with RxAndroid, Dagger 2 and Realm关于 Android 与 RxAndroid、Dagger 2 和 Realm 的问题
【发布时间】:2017-01-21 16:15:25
【问题描述】:

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

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

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

在大多数情况下,Realm 查询在 UIThread 上运行,所以为什么我不应该只将结果返回为 List 而不是

Observable<List<Bla>>

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

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;
    }
}

谢谢!

【问题讨论】:

    标签: android realm dagger-2 rx-android


    【解决方案1】:

    1.) RealmResults&lt;T&gt; 是自动更新的,如果您将RealmChangeListener 添加到您的RealmResults,那么只要基础数据发生更改,就会调用它。

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

    但是当您在RealmResults 上调用asObservable() 时,会创建一个Observable&lt;RealmResults&lt;T&gt;&gt;,它会自动附加一个RealmChangeListener,通知您更改,并在您取消订阅时将其删除。如果您检查默认的 RealmObservableFactory 实现,您可以查看确切的行为。

    因此,它确实是在结果中添加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.) Typically you should have 1 component / scope.

    编辑:

    1. 为什么我应该使用 Observable 作为 Realm 查询的返回类型?
    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&lt;RealmResults&lt;T&gt;&gt; because it is more reliable.

    3.) 不确定你的意思。没有@component appModule.class,Service Component 将无法工作,但在我的理解中应该是这样的。

    甚至不应该有ServiceComponent。只需@Component SingletonComponent

    【讨论】:

    • 1.我不是要使用 RealmResults,而是要返回像“public Bla getBla() { ...}”这样的对象,这会有点糟糕吗? 2. 是的,我做到了。谢谢! 3. 不知道你的意思。没有@component appModule.class,Service Component 是无法工作的,但在我的理解中应该是这样的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多