1

Basically my Android app have some meta data which need to report to backend server in different scenarios:

data class SearchMetaData(
    val searchId: String?,
    val isRateLimit: Boolean
)

In order to make the code clean, I make the minimal case as follows. All the report logic are similar, each of the function subscribe the metadata provider and get the value that need to report.

fun logEvent1() {
    fetchMetaData().observeOn(schedulers.mainThread()).subscribe({ metadata ->
        ...//lots of other events data here
        val sessionMetadata = SessionMetadata()
        sessionMetadata.id = metadata.searchId
        sessionMetadata.limiit = metadata.isRateLimit
        event1.session = sessionMetadata
        ...
        report(event1)
    })
}

fun logEvent2() {
    fetchMetaData().observeOn(schedulers.mainThread()).subscribe({ metadata ->
        ...//lots of other events data here
        val sessionMetadata = SessionMetadata()
        sessionMetadata.id = metadata.searchId
        sessionMetadata.limiit = metadata.isRateLimit
        event2.session = sessionMetadata
        ...
        report(event2)
    })
}

fun logEvent3() {
    fetchMetaData().observeOn(schedulers.mainThread()).subscribe({ metadata ->
        ...//lots of other events data here
        val sessionMetadata = SessionMetadata()
        sessionMetadata.id = metadata.searchId
        sessionMetadata.limiit = metadata.isRateLimit
        event3.session = sessionMetadata
        ...
        report(event3)
    })
}

My concern is every time we change the schema of metadata, we need to update all the logEventX , I was wondering if maybe we could extract all the subscribe in different functions and get the metadata?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
alwaysday1
  • 1,683
  • 5
  • 20
  • 36
  • 1
    could you provide a scenario when the metadata is changed and how you would change your `logEventX`? – Aram Sheroyan Sep 26 '19 at 07:42
  • 1
    Can you better explain the case because is not so clear what are you trying to achieve ? – Ricard Kollcaku Sep 26 '19 at 14:16
  • @AramSheroyan Thanks, I've updated some of the codes. For example, if `data class SearchMetaData` add a field `val searchType: Int` , we need to add `searchType` in every `logEvents` , if we forget to add to some of the `logEvents` , there might be exception. – alwaysday1 Sep 27 '19 at 01:46
  • @RicardKollcaku Thanks, I was wondering if maybe we could make the `val sessionMetadata = SessionMetadata()` to be global, in that case each `logEvents` function could use it directly. – alwaysday1 Sep 27 '19 at 01:48

1 Answers1

1

Consider an extension function using compose and doOnSuccess

Single<MetaData>.handleLogging() : Single<MetaData>{
  return compose{
     it.doOnSuccess{ metaData ->
         val sessionMetadata = SessionMetadata()
         sessionMetadata.id = metadata.searchId
         sessionMetadata.limiit = metadata.isRateLimit
         report(sessionMetaData)
     }
  }
}

//usage
fetchMetaData().handleLogging().subscribe{
  //other uncommon logic here.
}
Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91