Functional programming 如何为显示缓存网络数据的Android屏幕使用反应式编程?
我正试图找出如何将函数式编程应用到我正在Android应用程序中编写的功能上。阅读之后,反应式编程似乎非常适合此功能 场景:Functional programming 如何为显示缓存网络数据的Android屏幕使用反应式编程?,functional-programming,rx-java,reactive-programming,rx-android,Functional Programming,Rx Java,Reactive Programming,Rx Android,我正试图找出如何将函数式编程应用到我正在Android应用程序中编写的功能上。阅读之后,反应式编程似乎非常适合此功能 场景: -i-----------i---------------> screen_initialization: user goes into screen -N-----------Y---------------> check_cache: map() gives N if data not found in db, Y otherwise -q--------
-i-----------i---------------> screen_initialization: user goes into screen
-N-----------Y---------------> check_cache: map() gives N if data not found in db, Y otherwise
-q---------------------------> do_refresh_cache1: filter() out anything by N
----------------r------------> refresh_button_clicks
-q--------------r------------> do_refresh_cache2: merge() streams screen_initialization and do_refresh_cache1
-q--------------r------------> clear_cache: map() do_refresh_cache2, clearing datBAase
-cba|-----------cbad|--------> get_ids: flatMap() call API1 for ids (e.g. q results in 3 ids: a, b, c)
-CBA|-----------CBAD|--------> get_details: map() calls API2 for each id to get details
-CBA|-----------CBAD|--------> persist: map() persists each value (A, B, C, etc.) in the datBAase cache
----P---------------P--------> emit_ready: toPromise() emits a signal when we have details for each id
-q--------------r------------> do_load_from_cache: merge() streams screen_initialization and do_refresh_cache1
----P---------------P--------> wait_for_ready: fromPromise() waits for a signal when we have details for each id
----CBA|------------CBAD|----> load_from_cache: flatMap() query database for id details when wait_for_ready promise fullfilled
-------ABC|-------------ABCD|> display: somehow sort the results
-s--------------s------------> show_spinner: map() do_refresh_cache2 to show a spinner while making network calls
----------h-----------------h> hide_spinner: hide spinner when display stream finishes
-i-----------i---------------> screen_initialization: user goes into screen
-N-----------Y---------------> check_cache: map() gives N if data not found in db, Y otherwise
-q---------------------------> do_refresh_cache1: filter() out anything by N
----------------r------------> refresh_button_clicks
-q--------------r------------> do_refresh_cache2: merge() streams screen_initialization and do_refresh_cache1
-q--------------r------------> clear_cache: map() do_refresh_cache2, clearing datBAase
-cba|-----------cbad|--------> get_ids: flatMap() call API1 for ids (e.g. q results in 3 ids: a, b, c)
-CBA|-----------CBAD|--------> get_details: map() calls API2 for each id to get details
-CBA|-----------CBAD|--------> persist: map() persists each value (A, B, C, etc.) in the datBAase cache
----P---------------P--------> emit_ready: toPromise() emits a signal when we have details for each id
-q--------------r------------> do_load_from_cache: merge() streams screen_initialization and do_refresh_cache1
----P---------------P--------> wait_for_ready: fromPromise() waits for a signal when we have details for each id
----CBA|------------CBAD|----> load_from_cache: flatMap() query database for id details when wait_for_ready promise fullfilled
-------ABC|-------------ABCD|> display: somehow sort the results
-s--------------s------------> show_spinner: map() do_refresh_cache2 to show a spinner while making network calls
----------h-----------------h> hide_spinner: hide spinner when display stream finishes
缓存:
-i-----------i---------------> screen_initialization: user goes into screen
-N-----------Y---------------> check_cache: map() gives N if data not found in db, Y otherwise
-q---------------------------> do_refresh_cache1: filter() out anything by N
----------------r------------> refresh_button_clicks
-q--------------r------------> do_refresh_cache2: merge() streams screen_initialization and do_refresh_cache1
-q--------------r------------> clear_cache: map() do_refresh_cache2, clearing datBAase
-cba|-----------cbad|--------> get_ids: flatMap() call API1 for ids (e.g. q results in 3 ids: a, b, c)
-CBA|-----------CBAD|--------> get_details: map() calls API2 for each id to get details
-CBA|-----------CBAD|--------> persist: map() persists each value (A, B, C, etc.) in the datBAase cache
----P---------------P--------> emit_ready: toPromise() emits a signal when we have details for each id
-q--------------r------------> do_load_from_cache: merge() streams screen_initialization and do_refresh_cache1
----P---------------P--------> wait_for_ready: fromPromise() waits for a signal when we have details for each id
----CBA|------------CBAD|----> load_from_cache: flatMap() query database for id details when wait_for_ready promise fullfilled
-------ABC|-------------ABCD|> display: somehow sort the results
-s--------------s------------> show_spinner: map() do_refresh_cache2 to show a spinner while making network calls
----------h-----------------h> hide_spinner: hide spinner when display stream finishes
加载:
-i-----------i---------------> screen_initialization: user goes into screen
-N-----------Y---------------> check_cache: map() gives N if data not found in db, Y otherwise
-q---------------------------> do_refresh_cache1: filter() out anything by N
----------------r------------> refresh_button_clicks
-q--------------r------------> do_refresh_cache2: merge() streams screen_initialization and do_refresh_cache1
-q--------------r------------> clear_cache: map() do_refresh_cache2, clearing datBAase
-cba|-----------cbad|--------> get_ids: flatMap() call API1 for ids (e.g. q results in 3 ids: a, b, c)
-CBA|-----------CBAD|--------> get_details: map() calls API2 for each id to get details
-CBA|-----------CBAD|--------> persist: map() persists each value (A, B, C, etc.) in the datBAase cache
----P---------------P--------> emit_ready: toPromise() emits a signal when we have details for each id
-q--------------r------------> do_load_from_cache: merge() streams screen_initialization and do_refresh_cache1
----P---------------P--------> wait_for_ready: fromPromise() waits for a signal when we have details for each id
----CBA|------------CBAD|----> load_from_cache: flatMap() query database for id details when wait_for_ready promise fullfilled
-------ABC|-------------ABCD|> display: somehow sort the results
-s--------------s------------> show_spinner: map() do_refresh_cache2 to show a spinner while making network calls
----------h-----------------h> hide_spinner: hide spinner when display stream finishes
UI:
-i-----------i---------------> screen_initialization: user goes into screen
-N-----------Y---------------> check_cache: map() gives N if data not found in db, Y otherwise
-q---------------------------> do_refresh_cache1: filter() out anything by N
----------------r------------> refresh_button_clicks
-q--------------r------------> do_refresh_cache2: merge() streams screen_initialization and do_refresh_cache1
-q--------------r------------> clear_cache: map() do_refresh_cache2, clearing datBAase
-cba|-----------cbad|--------> get_ids: flatMap() call API1 for ids (e.g. q results in 3 ids: a, b, c)
-CBA|-----------CBAD|--------> get_details: map() calls API2 for each id to get details
-CBA|-----------CBAD|--------> persist: map() persists each value (A, B, C, etc.) in the datBAase cache
----P---------------P--------> emit_ready: toPromise() emits a signal when we have details for each id
-q--------------r------------> do_load_from_cache: merge() streams screen_initialization and do_refresh_cache1
----P---------------P--------> wait_for_ready: fromPromise() waits for a signal when we have details for each id
----CBA|------------CBAD|----> load_from_cache: flatMap() query database for id details when wait_for_ready promise fullfilled
-------ABC|-------------ABCD|> display: somehow sort the results
-s--------------s------------> show_spinner: map() do_refresh_cache2 to show a spinner while making network calls
----------h-----------------h> hide_spinner: hide spinner when display stream finishes
问题:
-i-----------i---------------> screen_initialization: user goes into screen
-N-----------Y---------------> check_cache: map() gives N if data not found in db, Y otherwise
-q---------------------------> do_refresh_cache1: filter() out anything by N
----------------r------------> refresh_button_clicks
-q--------------r------------> do_refresh_cache2: merge() streams screen_initialization and do_refresh_cache1
-q--------------r------------> clear_cache: map() do_refresh_cache2, clearing datBAase
-cba|-----------cbad|--------> get_ids: flatMap() call API1 for ids (e.g. q results in 3 ids: a, b, c)
-CBA|-----------CBAD|--------> get_details: map() calls API2 for each id to get details
-CBA|-----------CBAD|--------> persist: map() persists each value (A, B, C, etc.) in the datBAase cache
----P---------------P--------> emit_ready: toPromise() emits a signal when we have details for each id
-q--------------r------------> do_load_from_cache: merge() streams screen_initialization and do_refresh_cache1
----P---------------P--------> wait_for_ready: fromPromise() waits for a signal when we have details for each id
----CBA|------------CBAD|----> load_from_cache: flatMap() query database for id details when wait_for_ready promise fullfilled
-------ABC|-------------ABCD|> display: somehow sort the results
-s--------------s------------> show_spinner: map() do_refresh_cache2 to show a spinner while making network calls
----------h-----------------h> hide_spinner: hide spinner when display stream finishes
flatMap
用于将API1
的结果转换为API2
的内容应该会让您有很多工作要做。我建议您阅读(这一个与专门改装和Sqlbrite相关,但它可以很容易地应用于任何其他方面)。这个+flatMap
用于将API1
的结果转换为API2
的方法应该会让您有很多工作要做。