私は現在アプリに取り組んでおり、Android用の最新のライブラリであるRxAndroid、Dagger2、Realmを使用したいと考えていました。ネットで良い例が見つからなかったので、ここで聞いてみます。
public Observable<Bla> getBla() {
Bla b = realm.where(Bla.class).findFirst();
return Observable.just(b);
}
ほとんどの場合、レルムクエリはUIThreadで実行されるため、結果をリストではなくリストとして返す必要があるのはなぜですか。
Observable<List<Bla>>
たぶん誰かがこのlibの組み合わせのレルムで非常に良い例を知っていますか?
@Inject
TableService mTableService;
(以下のコード)
@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>
は自動更新であり、にを追加するRealmChangeListener
とRealmResults
、基になるデータが変更されるたびに呼び出されます。
private final RealmChangeListener listener = new RealmChangeListener() {
@Override
public void onChange(Object results) {
notifyDataSetChanged();
}
};
ただし、を呼び出すasObservable()
と、変更を通知するを自動的に追加し、登録を解除すると削除RealmResults
するObservable<RealmResults<T>>
が作成され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() {
}
}
編集:
- レルムクエリの戻り値の型としてObservableを使用する必要があるのはなぜですか?
public Observable<Bla> getBla() {
Bla b = realm.where(Bla.class).findFirst();
return Observable.just(b);
}
Realmは変更をリッスンしないため、これをRealmで使用することは実際には意味がありません。
使用する方が合理的です
public Observable<Blah> getBla() {
Blah blah = realm.where(Blah.class).findFirst();
if(blah == null) {
return Observable.empty();
} else {
return blah.asObservable();
}
}
が、個人的に私が使用することを助言するObservable<RealmResults<T>>
ことが、より信頼性があるため。
3.)どういう意味かわからない。サービスコンポーネントは@ componentappModule.classなしでは機能しませんが、私の理解ではそうなるはずです。
でもそこにはならないことServiceComponent
。ただ@Component SingletonComponent
。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加