I am using RXJava on Android for asynchronously access the database.
I want to save an object in my database. In this way, I created a method which take a final parameter (the object I want to save) and returns an Observable.
At this point I don't care to emit anything so I will call subscriber.onComplete()
at the end.
Here is my code:
public Observable saveEventLog(@NonNull final EventLog eventLog) {
return Observable.create(new Observable.OnSubscribe<Object>() {
@Override
public void call(Subscriber<? super Object> subscriber) {
DBEventLog log = new DBEventLog(eventLog);
log.save();
subscriber.onCompleted();
}
});
}
The thing is, I saw many answer using the final keyword for the parameter, but I would like to do this without it. The reason is I don't really like the approach of declare a final variable in order to use it in another thread.
Is there any alternative? Thanks.
We usually suggest avoiding the use of
create
because it may seem simple to use it but they usually violate the advanced requirements of RxJava. Instead, you should use one of the factory methods ofObservable
. In your case, thejust
factory method will get what you wanted: no final parameter: