Functional programming 如何为显示缓存网络数据的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--------

我正试图找出如何将函数式编程应用到我正在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---------------------------> 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
  • 用户点击设置
  • 列表中填充了一组从RESTAPI调用缓存(在磁盘上)的值
  • 用户可以通过单击刷新按钮刷新缓存
  • 要从REST API调用中获取值,有两个阶段:

  • API1-返回ID(需要两个HTTP请求头,“日期”和“授权”用于身份验证)
  • API2-给定id,返回有关该id的详细信息(无身份验证)
  • 由于我是反应式编程的新手,我想我应该从以反应式编程风格编写伪代码开始。然后我必须将伪代码翻译成适合Android的实际Java代码。要做到这一点,我需要知道哪种反应式编程库最适合我的任务

    为了开始编写伪代码,我将使用@andré-staltz在中使用的语法:

    用户操作:

    -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
    
  • Psuedo代码:如何使我的Psuedo代码符合要求
  • 真实代码:RxJava、RxAndroid或其他一些反应式编程库中应该使用哪些方法将psuedo代码转换为Android的真实代码
  • 我建议您阅读(其中一个与专门改装和Sqlbrite相关,但它可以很容易地应用于任何其他方面)。此+
    flatMap
    用于将
    API1
    的结果转换为
    API2
    的内容应该会让您有很多工作要做。我建议您阅读(这一个与专门改装和Sqlbrite相关,但它可以很容易地应用于任何其他方面)。这个+
    flatMap
    用于将
    API1
    的结果转换为
    API2
    的方法应该会让您有很多工作要做。