``` val resp = webservicesAPI.fetchLatest()...
# getting-started
d
Copy code
val resp = webservicesAPI.fetchLatest()
        resp?.let {
            repository.name = resp.name   
            repository.lastUpdate = resp.lastUpdate   
        }
d
The
let
lambda takes a param:
Copy code
fetchlatest()?.let { resp -> 
    repository.lastUpdate = resp.lastUpdate
    // etc
}
d
@diesieben07 thanks! what about if there are 2 lines in the let block, as I just amended ?
d
You can as many lines in the let block as you want, hence why I put "etc" there ๐Ÿ˜‰
๐Ÿ‘ 1
d
ok, I see! thanks!
m
This might be just stylistic, but I think
apply
is better here:
Copy code
webservicesAPI.fetchLatest()
  ?.apply {
      repository.name = name   
      repository.lastUpdate = lastUpdate   
  }
โœ”๏ธ 3
๐Ÿ™…๐Ÿปโ€โ™€๏ธ 1
d
@marstran thanks! so is
.apply
running only if
fetchLatest()
returns not null ?
m
Ah, didn't see the
?
. You still need that. I fixed the code.
d
ok, I see, thanks!
d
No,
apply
is like
let
, except that it uses a lambda wiht receiver instead of a lambda with parameter.
m
Yes, and it returns the input rather than the return value of the lambda.
d
Copy code
if (remoteData.lastUpdate == null) {
    fetchWeeklyCasesList()
}
do you also have a suggestion to simplify this code?
d
Not sure what you want to simplify about an if statement ๐Ÿ™‚ I would say that code is perfectly fine the way it is.
d
maybe writing
?
instead of
null
?
or maybe it can stay like this ๐Ÿ™‚
m
Yes, I think it should stay. The intention is clearer with the explicit null-check.
๐Ÿ‘ 1
j
Also you can think about creating a mapper, This is a great article on the why and mapping from dto's to domain objects is highly recommended, as, in short, it will make your code better testable. In that case your code could be in the likes of:
Copy code
val repository = webservicesAPI.fetchLatest()?.let { mapper.map(it) }
So I highly recommend reading this: https://proandroiddev.com/the-real-repository-pattern-in-android-efba8662b754