RxJava Operators

I am learning rxjava with the help of this article: http://blog.danlew.net/2014/09/22/grokking-rxjava-part-2/ and cannot reproduce the first example of this article I did the following:

Observable<List<String>> query(String text); //Gradle: error: missing method body, or declare abstract @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); query.subscribe(urls -> { //Gradle: error: cannot find symbol variable query for (String url : urls) { System.out.println(url); } }); } 

But I have errors that I added as comments. What have I done wrong?

+6
source share
2 answers

Java 8 lambdas aside, most people here are missing that your code will not compile regardless of RetroLambda or any other great tool that you find somewhere to get around the missing lambda function in Android ...

So, take a close look at your code, you even added some comments to the snippet that actually explain to you why you have some compilation errors:

1 You have a method with an empty body :

 Observable<List<String>> query(String text); 

So, add the method body to it and fix the problem. What do you want to do? You do not know yet? Then add a fictitious or empty body and execute it later:

 Observable<List<String>> query(String text) { return Observable.just(Arrays.asList("url1", "url2")); } 

2 There is no query variable in your code . You have a query method, and the syntax for using the methods requires the use of curly braces:

 query("whatever").subscribe(urls -> { for (String url : urls) { System.out.println(url); } }); 

Now add RetroLambda or use anonymous classes and you're done. Keep in mind that none of this will add much functionality to your code, but will only solve those compilation errors. Now ask yourself what you want to do in your query method and continue.

Note An Observable is a data stream, which basically means that you can get null elements, one element or many; all instances of the specified type. So your code seems to be expecting a stream of list of strings, if what you really want is a stream of strings, then replace Observable<List<String>> with Observable<String> .

+6
source

By Gradle: error: do you mean a compilation error? Most likely, you should enclose parentheses between query and .subscribe(urls -> { , since this is not a variable or class, but instead a method, so you must call it to get Observable to subscribe to.

Well, you also need to implement a query method to return an Observable , for example, as follows:

 private Observable<String> query() { return Observable.just("one", "two", "three"); } 

You will get another build error due to Java 8, but as mentioned in the comments, you can easily use retrolamda with gradle to fix this problem. Otherwise, you can use quick fixes for Android Studio to convert java 8 lambdas to anonymous java 6 classes.

+3
source

Source: https://habr.com/ru/post/979788/


All Articles