lecture #7 reactive programmingdan/ma/lecture7.pdfwhy reactive? • unless you can model your entire...
TRANSCRIPT
![Page 1: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/1.jpg)
Lecture #7 Reactive Programming
Mobile Applications 2019-2020
![Page 2: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/2.jpg)
Why Reactive?
• Unless you can model your entire system synchronously ...
![Page 3: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/3.jpg)
Why Reactive?
• Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming.
![Page 4: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/4.jpg)
Why Reactive?
interface UserManager { fun getUser(): User }
![Page 5: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/5.jpg)
Why Reactive?
interface UserManager { fun getUser(): User fun setName(name: String) fun setAge(age: Int) }
![Page 6: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/6.jpg)
Why Reactive?interface UserManager { fun getUser(): User fun setName(name: String) fun setAge(age: Int) }
val um: UserManager = UserManagerImpl() logd(um.getUser())
![Page 7: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/7.jpg)
Why Reactive?interface UserManager { fun getUser(): User fun setName(name: String) fun setAge(age: Int) }
val um: UserManager = UserManagerImpl() logd(um.getUser())
um.setName("John Doe") logd(um.getUser())
![Page 8: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/8.jpg)
Why Reactive?interface UserManager { fun getUser(): User fun setName(name: String) fun setAge(age: Int) }
val um: UserManager = UserManagerImpl() logd(um.getUser())
um.setName("John Doe") logd(um.getUser())
![Page 9: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/9.jpg)
Why Reactive?
interface UserManager { fun getUser(): User fun setName(name: String) fun setAge(age: Int) }
![Page 10: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/10.jpg)
Why Reactive?
interface UserManager { fun getUser(): User fun setName(name: String) fun setAge(age: Int) }
![Page 11: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/11.jpg)
Why Reactive?
interface UserManager { fun getUser(): User fun setName(name: String) fun setAge(age: Int) }
um.setName("John Doe") logd(um.getUser())
![Page 12: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/12.jpg)
Why Reactive?
interface UserManager { fun getUser(): User fun setName(name: String) fun setAge(age: Int) }
um.setName("John Doe") logd(um.getUser())
![Page 13: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/13.jpg)
Why Reactive?
interface UserManagerV2 { fun getUser(): User fun setName(name: String, callback: Runnable) fun setAge(age: Int, callback: Runnable) }
![Page 14: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/14.jpg)
Why Reactive?interface UserManagerV2 { fun getUser(): User fun setName(name: String, callback: Runnable) fun setAge(age: Int, callback: Runnable) }
val um: UserManagerV2 = UserManagerV2Impl() logd(um.getUser())
![Page 15: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/15.jpg)
Why Reactive?interface UserManagerV2 { fun getUser(): User fun setName(name: String, callback: Runnable) fun setAge(age: Int, callback: Runnable) }
val um: UserManagerV2 = UserManagerV2Impl() logd(um.getUser())
um.setName("John Doe", Runnable { logd(um.getUser()) })
![Page 16: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/16.jpg)
Why Reactive?interface UserManagerV2 { fun getUser(): User fun setName(name: String, callback: Runnable) fun setAge(age: Int, callback: Runnable) }
val um: UserManagerV2 = UserManagerV2Impl() logd(um.getUser())
um.setName("John Doe", Runnable { logd(um.getUser()) })
![Page 17: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/17.jpg)
Why Reactive?interface UserManagerV2 { fun getUser(): User fun setName(name: String, callback: Runnable) fun setAge(age: Int, callback: Runnable) }
val um: UserManagerV2 = UserManagerV2Impl() logd(um.getUser())
um.setName("John Doe", Runnable { logd(um.getUser()) })
Fail
Fail
Fail
Fail
?
?? ? ?
?
?
![Page 18: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/18.jpg)
Why Reactive?
interface UserManagerV3 { fun getUser(): User fun setName(name: String, listener: Listener): void fun setAge(age: Int, listener: Listener): void
interface Listener { fun success(user: User) fun failed(error: UserException) } }
![Page 19: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/19.jpg)
Why Reactive?interface UserManagerV3 { fun getUser(): User fun setName(name: String, listener: Listener): void fun setAge(age: Int, listener: Listener): void
interface Listener { fun success(user: User) fun failed(error: UserException) } }
val um: UserManagerV3 = UserManagerV3Impl() logd(um.getUser())
um.setName("John Doe", object : UserManagerV3.Listener { override fun success(user: User) { logd(user) }
override fun failed(error: UserException) { loge("Unable to update the user details", error) } })
![Page 20: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/20.jpg)
Why Reactive?val um: UserManagerV3 = UserManagerV3Impl() logd(um.getUser())
um.setName("John Doe", object : UserManagerV3.Listener { override fun success(user: User) { logd(user) }
override fun failed(error: UserException) { loge("Unable to update the user details", error) } })
um.setAge(42, object : UserManagerV3.Listener { override fun success(user: User) { logd(user) }
override fun failed(error: UserException) { loge("Unable to update the user details", error) } })
![Page 21: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/21.jpg)
Why Reactive?um.setName("John Doe", object : UserManagerV3.Listener { override fun success(user: User) { um.setAge(42, object : UserManagerV3.Listener { override fun success(user: User) { logd(user) }
override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) }
override fun failed(error: UserException) { loge("Unable to update the user details", error) } })
![Page 22: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/22.jpg)
Why Reactive?um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { logd(user) }
override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) }
override fun failed(error: UserException) { loge("Unable to update the user details", error) } })
![Page 23: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/23.jpg)
Why Reactive?
um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { logd(user) }
override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) }
override fun failed(error: UserException) { loge("Unable to update the user details", error) } })
class UserActivity : AppCompatActivity() { val um: UserManager = UserManagerImpl()
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user)
} }
![Page 24: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/24.jpg)
Why Reactive?
textView.text = user.name
class UserActivity : AppCompatActivity() { val um: UserManager = UserManagerImpl() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user) um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { textView.text = user.name logd(user) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } })
} override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } }
![Page 25: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/25.jpg)
Why Reactive?class UserActivity : AppCompatActivity() { val um: UserManager = UserManagerImpl() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user) um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { if (!isDestroyed) { textView.text = user.name } logd(user) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } }
![Page 26: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/26.jpg)
Why Reactive?class UserActivity : AppCompatActivity() { val um: UserManager = UserManagerImpl() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user) um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { if (!isDestroyed) { textView.text = user.name } logd(user) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } }
“A small leak will sink a great ship.” Benjamin Franklin
![Page 27: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/27.jpg)
Why Reactive?class UserActivity : AppCompatActivity() { val um: UserManager = UserManagerImpl() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user) um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { if (!isDestroyed) { textView.text = user.name } logd(user) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } }
“A small leak will sink a great ship.” Benjamin Franklin
https://github.com/square/leakcanary
![Page 28: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/28.jpg)
Why Reactive?class UserActivity : AppCompatActivity() { val um: UserManager = UserManagerImpl() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user) um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { if (!isDestroyed) { textView.text = user.name } logd(user) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } }
![Page 29: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/29.jpg)
Why Reactive?class UserActivity : AppCompatActivity() { val um: UserManager = UserManagerImpl() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user) um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { if (!isDestroyed) { textView.text = user.name } logd(user) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } }
![Page 30: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/30.jpg)
Why Reactive?class UserActivity : AppCompatActivity() { val um: UserManager = UserManagerImpl() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user) um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { runOnUiThread { if (!isDestroyed) { textView.text = user.name } } } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } //... }) } }
![Page 31: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/31.jpg)
Why Reactive?
![Page 32: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/32.jpg)
Why Reactive?
![Page 33: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/33.jpg)
Why Reactive?
![Page 34: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/34.jpg)
Why Reactive?
![Page 35: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/35.jpg)
Why Reactive?
![Page 36: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/36.jpg)
Why Reactive?
![Page 37: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/37.jpg)
Why Reactive?
![Page 38: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/38.jpg)
Why Reactive?
UI
![Page 39: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/39.jpg)
Why Reactive?
UI
LiveData
![Page 40: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/40.jpg)
Why Reactive?
UI
LiveData LiveData
![Page 41: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/41.jpg)
Why Reactive?
UI
LiveData LiveData
LiveData
![Page 42: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/42.jpg)
Why Reactive?
UI
LiveData LiveData
LiveData
![Page 43: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/43.jpg)
Why Reactive?
UI
![Page 44: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/44.jpg)
Rx{Java|Kotlin|Swift|Dart}
• A set of classes for representing sources of data.
• A set of classes for listening to data sources.
• A set of methods for modifying and composing the data.
http://reactivex.io/
![Page 45: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/45.jpg)
Sources
![Page 46: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/46.jpg)
Sources
![Page 47: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/47.jpg)
Sources• Synchronous or
asynchronous.
![Page 48: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/48.jpg)
Sources• Synchronous or
asynchronous.
• Single item, many items, or empty.
![Page 49: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/49.jpg)
Sources• Synchronous or
asynchronous.
• Single item, many items, or empty.
• Terminates with an error or succeeds to completion.
![Page 50: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/50.jpg)
Sources• Synchronous or
asynchronous.
• Single item, many items, or empty.
• Terminates with an error or succeeds to completion.
• May never terminate!
![Page 51: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/51.jpg)
Sources• Synchronous or
asynchronous.
• Single item, many items, or empty.
• Terminates with an error or succeeds to completion.
• May never terminate!
• Just an implementation of the Observer pattern.
![Page 52: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/52.jpg)
Sources
• Observable<T>
• Flowable<T>
![Page 53: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/53.jpg)
Sources
• Observable<T>
• Emits 0 to N items.
• Terminates with complete or error.
• Flowable<T>
• Emits 0 to N items.
• Terminates with complete or error.
![Page 54: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/54.jpg)
Sources• Observable<T>
• Emits 0 to N items.
• Terminates with complete or error.
• Does not have backpressure.
• Flowable<T>
• Emits 0 to N items.
• Terminates with complete or error.
• Has backpressure.
![Page 55: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/55.jpg)
Sources• Observable<T>
• Emits 0 to N items.
• Terminates with complete or error.
• Does not have backpressure.
• Flowable<T>
• Emits 0 to N items.
• Terminates with complete or error.
• Has backpressure.
![Page 56: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/56.jpg)
Flowable vs. Observableval events: Observable<MotionEvent> = RxView.touches(paintView);
![Page 57: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/57.jpg)
Flowable vs. Observableval events: Observable<MotionEvent> = RxView.touches(paintView);
val users: Observable<User> = db.query("select * from ...");
![Page 58: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/58.jpg)
Flowable vs. Observableval events: Observable<MotionEvent> = RxView.touches(paintView);
val users: Flowable<User> = db.query("select * from ...");
![Page 59: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/59.jpg)
Flowable vs. ObservableObservable<MotionEvent> Flowable<User>
interface Observer<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(d: Disposable) }
interface Subscriber<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(s: Subscription) }
![Page 60: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/60.jpg)
Flowable vs. ObservableObservable<MotionEvent> Flowable<User>
interface Observer<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(d: Disposable) }
interface Subscriber<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(s: Subscription) }
![Page 61: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/61.jpg)
Flowable vs. ObservableObservable<MotionEvent> Flowable<User>
interface Observer<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(d: Disposable) }
interface Subscriber<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(s: Subscription) }
![Page 62: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/62.jpg)
Flowable vs. ObservableObservable<MotionEvent> Flowable<User>
interface Observer<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(d: Disposable) }
interface Subscriber<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(s: Subscription) }
![Page 63: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/63.jpg)
Flowable vs. ObservableObservable<MotionEvent> Flowable<User>
interface Observer<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(d: Disposable) }
interface Subscriber<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(s: Subscription) }
interface Disposable{ fun dispose() }
interface Subscription{ fun cancel() fun request(r: Long) }
![Page 64: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/64.jpg)
Sources
interface UserManager { fun getUser(): User fun setName(name: String) fun setAge(age: Int) }
![Page 65: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/65.jpg)
Sources
interface UserManager { fun getUser(): Observable<User> fun setName(name: String) fun setAge(age: Int) }
![Page 66: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/66.jpg)
Specialized Sources
![Page 67: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/67.jpg)
Specialized Sources• Encoding subsets of Observable into the type
system:
![Page 68: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/68.jpg)
Specialized Sources• Encoding subsets of Observable into the type
system:
• Single
• Either succeeds with an item or an error.
• No backpressure support.
1
![Page 69: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/69.jpg)
Specialized Sources• Encoding subsets of Observable into the type
system:
• Single
• Either succeeds with an item or an error.
• No backpressure support.
• Completable
• Either completes or errors. Has no items!
• No backpressure support.
![Page 70: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/70.jpg)
Specialized Sources• Encoding subsets of Observable into the type
system:
• Single
• Either succeeds with an item or an error.
• No backpressure support.
• Completable
• Either completes or errors. Has no items!
• No backpressure support.
• Maybe
• Either succeeds with an item, completes with no items, or error.
• No backpressure support.
1
0
![Page 71: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/71.jpg)
Specialized Sources• Encoding subsets of Observable into the type
system:
• Single
• Either succeeds with an item or an error.
• No backpressure support.
• Completable
• Either completes or errors. Has no items!
• No backpressure support.
• Maybe
• Either succeeds with an item, completes with no items, or error.
• No backpressure support.
1
0
![Page 72: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/72.jpg)
Specialized Sources• Encoding subsets of Observable into the type
system:
• Single
• Either succeeds with an item or an error.
• No backpressure support.
• Completable
• Either completes or errors. Has no items!
• No backpressure support.
• Maybe
• Either succeeds with an item, completes with no items, or error.
• No backpressure support.
1
0
Runnable
![Page 73: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/73.jpg)
Specialized Sources• Encoding subsets of Observable into the type
system:
• Single
• Either succeeds with an item or an error.
• No backpressure support.
• Completable
• Either completes or errors. Has no items!
• No backpressure support.
• Maybe
• Either succeeds with an item, completes with no items, or error.
• No backpressure support.
1
0
Runnable
Optional
![Page 74: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/74.jpg)
Sources
interface UserManager { fun getUser(): Observable<User> fun setName(name: String) fun setAge(age: Int) }
![Page 75: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/75.jpg)
Sources
interface UserManager { fun getUser(): Observable<User> fun setName(name: String): Completable fun setAge(age: Int): Completable }
![Page 76: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/76.jpg)
Creating Sources
Flowable.just("Hello") Flowable.just("Hello", "World")
Observable.just("Hello") Observable.just("Hello", "World")
Maybe.just("Hello")
Single.just("Hello")
![Page 77: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/77.jpg)
Creating SourcesFlowable.just("Hello") Flowable.just("Hello", "World")
Observable.just("Hello") Observable.just("Hello", "World")
Maybe.just("Hello")
Single.just("Hello")
val array = arrayListOf("Hello", "World") val list = array.toList()
Flowable.fromArray(array) Flowable.fromIterable(list)
Observable.fromArray(array) Observable.fromIterable(list)
![Page 78: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/78.jpg)
Creating SourcesFlowable.just("Hello") Flowable.just("Hello", "World")
Observable.just("Hello") Observable.just("Hello", "World")
Maybe.just("Hello")
Single.just("Hello")
val array = arrayListOf("Hello", "World") val list = array.toList()
Flowable.fromArray(array) Flowable.fromIterable(list)
Observable.fromArray(array) Observable.fromIterable(list)
Observable.fromCallable { setName() }
![Page 79: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/79.jpg)
Creating Sources
val url = "https://example.com"
val request = Request.Builder().url(url).build() val client = OkHttpClient() Observable.fromCallable { client.newCall(request).execute() }
![Page 80: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/80.jpg)
Create Sources
Observable.create(ObservableOnSubscribe<String> { it.onNext("Hello") it.onComplete() })
![Page 81: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/81.jpg)
Create Sources
Observable.create(ObservableOnSubscribe<String> { it.onNext("Hello") it.onComplete() })
Observable.create(ObservableOnSubscribe<String>( function = fun(it: ObservableEmitter<String>) { it.onNext("Hello") it.onComplete() }))
![Page 82: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/82.jpg)
Create Sources
Observable.create(ObservableOnSubscribe<String> { it.onNext("Hello") it.onComplete() })
Observable.create(ObservableOnSubscribe<String>( function = fun(it: ObservableEmitter<String>) { it.onNext("Hello") it.onComplete() }))
Observable.create<String> { it.onNext("Hello") it.onComplete() }
![Page 83: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/83.jpg)
Create Sources
Observable.create<String> { it.onNext("Hello") it.onNext("World") it.onComplete() }
![Page 84: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/84.jpg)
Create Sources
Observable.create<View> { it.setCancellable { textView.setOnClickListener(null) } textView.setOnClickListener { v -> it.onNext(v) } }
![Page 85: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/85.jpg)
Create SourcesObservable.create<View> { it.setCancellable { textView.setOnClickListener(null) } textView.setOnClickListener { v -> it.onNext(v) } }
val request = Request.Builder().url(url).build() val client = OkHttpClient()
Observable.create<String> { val call = client.newCall(request) it.setCancellable { call.cancel() } call.enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) { it.onError(e) }
override fun onResponse(call: Call, response: Response) { it.onNext(response.body().toString()) it.onComplete() } }) }
![Page 86: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/86.jpg)
Observing SourcesObservable<MotionEvent> Flowable<User>
interface Observer<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(d: Disposable) }
interface Subscriber<T>{ fun onNext(t: T) fun onComplete() fun onError(t: Throwable) fun onSubscribe(s: Subscription) }
interface Disposable{ fun dispose() }
interface Subscription{ fun cancel() fun request(r: Long) }
![Page 87: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/87.jpg)
Observing Sources
![Page 88: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/88.jpg)
Observing Sources
val observable: Observable<String> = Observable.just("Hello")
observable.subscribe(object: DisposableObserver<String>(){ override fun onComplete() { //... }
override fun onNext(t: String) { //... }
override fun onError(e: Throwable) { //... } })
![Page 89: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/89.jpg)
Observing Sources
val observable: Observable<String> = Observable.just("Hello")
observable.subscribe(object: DisposableObserver<String>(){ override fun onComplete() { //... }
override fun onNext(t: String) { //... }
override fun onError(e: Throwable) { //... } })
How to we dispose?
![Page 90: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/90.jpg)
Observing Sourcesval observable: Observable<String> = Observable.just("Hello")
val observer = object : DisposableObserver<String>() { override fun onComplete() { //... }
override fun onNext(t: String) { //... }
override fun onError(e: Throwable) { //... } } observable.subscribe(observer)
observer.dispose()
![Page 91: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/91.jpg)
Observing Sourcesval observable: Observable<String> = Observable.just("Hello")
val disposable = observable.subscribeWith(object : DisposableObserver<String>() { override fun onComplete() { //... }
override fun onNext(t: String) { //... }
override fun onError(e: Throwable) { //... } })
disposable.dispose()
![Page 92: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/92.jpg)
Rx{Java|Kotlin|Swift|Dart}
• A set of classes for representing sources of data.
• A set of classes for listening to data sources.
• A set of methods for modifying and composing the data.
![Page 93: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/93.jpg)
Rx{Java|Kotlin|Swift|Dart}
• A set of classes for representing sources of data.
• A set of classes for listening to data sources.
• A set of methods for modifying and composing the data.
![Page 94: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/94.jpg)
Operators
• Manipulate or combine data in some way.
• Manipulate threading in some way.
• Manipulate emissions in some way.
![Page 95: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/95.jpg)
Operators
val greeting = "Hello" val yelling = greeting.toUpperCase()
![Page 96: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/96.jpg)
Operators
val greeting = Observable.just("Hello") val yelling = greeting.toUpperCase()
![Page 97: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/97.jpg)
Operators
val greeting = Observable.just("Hello") val yelling = greeting.map { it.toUpperCase() }
![Page 98: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/98.jpg)
Operatorsclass UserActivity : AppCompatActivity() { val um: UserManager = UserManagerImpl() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user) um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { runOnUiThread { if (!isDestroyed) { textView.text = user.name } } } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } //... }) } }
![Page 99: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/99.jpg)
Operatorsclass UserActivity : AppCompatActivity() { val um: UserManager = UserManagerImpl() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user) um.setName("John Doe", object : UserManager.Listener { override fun success(user: User) { um.setAge(42, object : UserManager.Listener { override fun success(user: User) { runOnUiThread { if (!isDestroyed) { textView.text = user.name } } } override fun failed(error: UserException) { loge("Unable to update the user details", error) } }) } //... }) } }
![Page 100: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/100.jpg)
Operators
val user: Observable<User> = um.getUser() val mainThreadUser = user.observeOn(AndroidSchedulers.mainThread())
![Page 101: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/101.jpg)
Operators
val user: Observable<User> = um.getUser() val mainThreadUser = user.observeOn(AndroidSchedulers.mainThread())
val url = "https://example.com"
val request = Request.Builder().url(url).build() val client = OkHttpClient()
val reponse = Observable.fromCallable { client.newCall(request).execute() } .subscribeOn(Schedulers.io()).map { it.body()?.string() } .flatMap { Observable.fromArray(it.split(" ")) } .observeOn(AndroidSchedulers.mainThread())
![Page 102: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/102.jpg)
Operators
Observable
Observable
![Page 103: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/103.jpg)
Operators
Observable
Observable
1 2 3 4 5 6 7 8 9
![Page 104: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/104.jpg)
Operators
Observable
Observable
1 2 3 4 5 6 7 8 9
1
first()
![Page 105: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/105.jpg)
Operators
1 2 3 4 5 6 7 8 9
1
List<Long>
Long
get(0)
![Page 106: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/106.jpg)
Operators
1 2 3 4 5 6 7 8 9
1
first()
Observable
Single
![Page 107: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/107.jpg)
Operators
first()
Observable
Single
![Page 108: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/108.jpg)
Operators
first()
Observable
Single
![Page 109: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/109.jpg)
Operators
Observable
firstElement()
Maybe
![Page 110: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/110.jpg)
Operators
Observable 1 2 3 4 5 6 7 8 9
![Page 111: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/111.jpg)
Operators
Observable
Completable
1 2 3 4 5 6 7 8 9
ignoreElements()
![Page 112: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/112.jpg)
Operators
1 2 3 4 5 6 7 8 9
1
first()
Single
FlowableFlowable
![Page 113: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/113.jpg)
Operators
firstElement()
Maybe
FlowableFlowable
![Page 114: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/114.jpg)
Operators
Flowable
Completable
1 2 3 4 5 6 7 8 9
ignoreElements()
![Page 115: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/115.jpg)
Operators
Flowable
Completable
1 2 3 4 5 6 7 8 9
ignoreElements()
http://reactivex.io/documentation/operators.html
![Page 116: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/116.jpg)
Being Reactive
um.getUser() .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(object: DisposableObserver<User>(){ override fun onNext(user: User) { }
override fun onError(e: Throwable) { }
override fun onComplete() { } })
![Page 117: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/117.jpg)
Being Reactive
um.getUser() .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(object: DisposableObserver<User>(){ override fun onNext(user: User) { textView.text = user.toString() }
override fun onError(e: Throwable) { }
override fun onComplete() { } })
![Page 118: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/118.jpg)
Being Reactive
// onCreate val disposables = CompositeDisposable() disposables.add(um.getUser() .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(object: DisposableObserver<User>(){ override fun onNext(user: User) { textView.text = user.toString() }
override fun onError(e: Throwable) { }
override fun onComplete() { } }))
// onDestroy disposable.dispose()
![Page 119: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/119.jpg)
Dependencies
dependencies { implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0' implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' }
![Page 120: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/120.jpg)
Options
![Page 121: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/121.jpg)
Options
• Callbacks
![Page 122: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/122.jpg)
Options
• Callbacks
• Futures
![Page 123: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/123.jpg)
Options
• Callbacks
• Futures
• Promises
![Page 124: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/124.jpg)
Options
• Callbacks
• Futures
• Promises
• Rx
![Page 125: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/125.jpg)
Options
• Callbacks
• Futures
• Promises
• Rx
• Coroutines
https://kotlinlang.org/docs/reference/coroutines-overview.html
![Page 126: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/126.jpg)
Coroutinesfun requestToken(): Token { // make a token request and waits return token }
fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }
![Page 127: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/127.jpg)
Coroutinesfun requestToken(): Token { // make a token request // block the thread while waiting for result return token }
fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }
![Page 128: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/128.jpg)
Coroutinesfun requestToken(): Token { // make a token request // block the thread while waiting for result return token }
fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }
![Page 129: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/129.jpg)
Coroutinessuspend fun requestToken(): Token { // make a token request and suspends return token }
fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }
![Page 130: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/130.jpg)
Coroutinessuspend fun requestToken(): Token { // make a token request and suspends return token }
suspend fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }
![Page 131: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/131.jpg)
Coroutinessuspend fun requestToken(): Token { // make a token request and suspends return token }
suspend fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
suspend fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }
![Page 132: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/132.jpg)
Bonuses
for ((token, item) in list) { createPost(token, item) }
Regular loops:
![Page 133: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/133.jpg)
Bonuses
for ((token, item) in list) { createPost(token, item) }
Regular loops:
![Page 134: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/134.jpg)
Bonusesfor ((token, item) in list) { createPost(token, item) }
Regular loops:
Regular exception handling:try { createPost(token, item) } catch (e: BadTokenException) { // ... }
![Page 135: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/135.jpg)
Bonusesfor ((token, item) in list) { createPost(token, item) }
Regular loops:
Regular exception handling:try { createPost(token, item) } catch (e: BadTokenException) { // ... }
Regular higher-order function:
file.readLines().forEach { line -> createPost(token, line.toItem()) }
![Page 136: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/136.jpg)
Bonusesfor ((token, item) in list) { createPost(token, item) }
Regular loops:
Regular exception handling:try { createPost(token, item) } catch (e: BadTokenException) { // ... }
Regular higher-order function:
file.readLines().forEach { line -> createPost(token, line.toItem()) }
file.readLines().forEach { line -> createPost(token, line.toItem()) }
Any of: forEach, let, apply, repeat, filter, map, use, etc.
![Page 137: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/137.jpg)
Builderssuspend fun requestToken(): Token { // make a token request and suspends return token }
suspend fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
suspend fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }
![Page 138: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/138.jpg)
Builderssuspend fun requestToken(): Token { // make a token request and suspends return token }
suspend fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }
![Page 139: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/139.jpg)
Builderssuspend fun requestToken(): Token { // make a token request and suspends return token }
suspend fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }
![Page 140: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/140.jpg)
Builderssuspend fun requestToken(): Token { // make a token request and suspends return token }
suspend fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
fun postItem(item: Item) { GlobalScope.launch { val token = requestToken() val post = createPost(token, item) processPost(post) } }
![Page 141: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/141.jpg)
Builderssuspend fun requestToken(): Token { // make a token request and suspends return token }
suspend fun createPost(token: Token, item: Item): Post { logd("Posting an $item using $token") //sends the item to the server and waits return post }
fun processPost(post: Post) { // processing the post logd("Processing a $post") }
fun postItem(item: Item) { GlobalScope.launch(Dispatchers.Main) { val token = requestToken() val post = createPost(token, item) processPost(post) } }
![Page 142: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/142.jpg)
Builders
public fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit ): Job { val newContext = newCoroutineContext(context) val coroutine = if (start.isLazy) LazyStandaloneCoroutine(newContext, block) else StandaloneCoroutine(newContext, active = true) coroutine.start(start, coroutine, block) return coroutine }
![Page 143: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/143.jpg)
Dependencies
dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2' implementation “org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0" }
![Page 144: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/144.jpg)
Usageclass MyViewModel : ViewModel() { private val _result = MutableLiveData<String>() val result: LiveData<String> = _result
init { viewModelScope.launch { val computationalResult = doComputation() _result.value = computationalResult } }}
![Page 145: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/145.jpg)
Usageclass MyViewModel : ViewModel() { private val _result = MutableLiveData<String>() val result: LiveData<String> = _result
init { viewModelScope.launch { val computationalResult = doComputation() _result.value = computationalResult } }}
![Page 146: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/146.jpg)
Usageclass MyViewModel : ViewModel() { private val _result = MutableLiveData<String>() val result: LiveData<String> = _result
init { viewModelScope.launch { val computationalResult = doComputation() _result.value = computationalResult } }}
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-rc01"
![Page 147: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/147.jpg)
Usage
class MyNewViewModel : ViewModel() { val result = liveData { emit(doComputation()) }}
![Page 148: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/148.jpg)
Usage
class MyNewViewModel : ViewModel() { val result = liveData { emit(doComputation()) }}
![Page 149: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/149.jpg)
Usage
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-rc01"
class MyNewViewModel : ViewModel() { val result = liveData { emit(doComputation()) }}
![Page 150: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/150.jpg)
Flow
suspend fun foo(): List<Int> { delay(1000) // pretend we are doing something asynchronous here return listOf(1, 2, 3)}
fun main() = runBlocking<Unit> { foo().forEach { value -> println(value) }}
![Page 151: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/151.jpg)
Flow
suspend fun foo(): List<Int> { delay(1000) // pretend we are doing something asynchronous here return listOf(1, 2, 3)}
fun main() = runBlocking<Unit> { foo().forEach { value -> println(value) }}
Output:
1 2 3
![Page 152: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/152.jpg)
Flowfun foo(): Flow<Int> = flow { // flow builder for (i in 1..3) { delay(100) // pretend we are doing something useful here emit(i) // emit next value }}fun main() = runBlocking<Unit> { // Launch a concurrent coroutine to check if the main thread is blocked launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } // Collect the flow foo().collect { value -> println(value) }}
![Page 153: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/153.jpg)
Flowfun foo(): Flow<Int> = flow { // flow builder for (i in 1..3) { delay(100) // pretend we are doing something useful here emit(i) // emit next value }}fun main() = runBlocking<Unit> { // Launch a concurrent coroutine to check if the main thread is blocked launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } // Collect the flow foo().collect { value -> println(value) }}
![Page 154: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/154.jpg)
Flowfun foo(): Flow<Int> = flow { // flow builder for (i in 1..3) { delay(100) // pretend we are doing something useful here emit(i) // emit next value }}fun main() = runBlocking<Unit> { // Launch a concurrent coroutine to check if the main thread is blocked launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } // Collect the flow foo().collect { value -> println(value) }}
![Page 155: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/155.jpg)
Flowfun foo(): Flow<Int> = flow { // flow builder for (i in 1..3) { delay(100) // pretend we are doing something useful here emit(i) // emit next value }}fun main() = runBlocking<Unit> { // Launch a concurrent coroutine to check if the main thread is blocked launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } // Collect the flow foo().collect { value -> println(value) }}
![Page 156: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/156.jpg)
Flowfun foo(): Flow<Int> = flow { // flow builder for (i in 1..3) { delay(100) // pretend we are doing something useful here emit(i) // emit next value }}fun main() = runBlocking<Unit> { // Launch a concurrent coroutine to check if the main thread is blocked launch { for (k in 1..3) { println("I'm not blocked $k") delay(100) } } // Collect the flow foo().collect { value -> println(value) }}
Output:
I'm not blocked 1 1 I'm not blocked 2 2 I'm not blocked 3 3
![Page 157: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/157.jpg)
Flows are coldfun foo(): Flow<Int> = flow { println("Flow started") for (i in 1..3) { delay(100) emit(i) }}fun main() = runBlocking<Unit> { println("Calling foo...") val flow = foo() println("Calling collect...") flow.collect { value -> println(value) } println("Calling collect again...") flow.collect { value -> println(value) }}
![Page 158: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/158.jpg)
Flows are coldfun foo(): Flow<Int> = flow { println("Flow started") for (i in 1..3) { delay(100) emit(i) }}fun main() = runBlocking<Unit> { println("Calling foo...") val flow = foo() println("Calling collect...") flow.collect { value -> println(value) } println("Calling collect again...") flow.collect { value -> println(value) }}
Output:
Calling foo... Calling collect... Flow started 1 2 3 Calling collect again... Flow started 1 2 3
![Page 159: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/159.jpg)
Flow Cancellation
fun fooCancellation(): Flow<Int> = flow { for (i in 1..3) { delay(100) println("Emitting $i") emit(i) }}
fun main() = runBlocking<Unit> { withTimeoutOrNull(250) { // Timeout after 250ms fooCancellation().collect { value -> println(value) } } println("Done")}
![Page 160: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/160.jpg)
Flow Cancellation
fun fooCancellation(): Flow<Int> = flow { for (i in 1..3) { delay(100) println("Emitting $i") emit(i) }}
fun main() = runBlocking<Unit> { withTimeoutOrNull(250) { // Timeout after 250ms fooCancellation().collect { value -> println(value) } } println("Done")}
Output:
Emitting 1 1 Emitting 2 2 Done
![Page 161: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/161.jpg)
Flow Operators
suspend fun performRequest(request: Int): String { delay(1000) // imitate long-running asynchronous work return "response $request"}
fun main() = runBlocking<Unit> { (1..3).asFlow() // a flow of requests .map { request -> performRequest(request) } .collect { response -> println(response) }}
![Page 162: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/162.jpg)
Flow Operators
suspend fun performRequest(request: Int): String { delay(1000) // imitate long-running asynchronous work return "response $request"}
fun main() = runBlocking<Unit> { (1..3).asFlow() // a flow of requests .map { request -> performRequest(request) } .collect { response -> println(response) }}
Output:
response 1response 2response 3
![Page 163: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/163.jpg)
Transform Operatorsuspend fun performRequest(request: Int): String { delay(1000) // imitate long-running asynchronous work return "response $request"}(1..3).asFlow() // a flow of requests .transform { request -> emit("Making request $request") emit(performRequest(request)) } .collect { response -> println(response) }
![Page 164: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/164.jpg)
Transform Operatorsuspend fun performRequest(request: Int): String { delay(1000) // imitate long-running asynchronous work return "response $request"}(1..3).asFlow() // a flow of requests .transform { request -> emit("Making request $request") emit(performRequest(request)) } .collect { response -> println(response) }
Output:
Making request 1response 1Making request 2response 2Making request 3response 3
![Page 165: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/165.jpg)
Size-limiting Operatorsfun numbers(): Flow<Int> = flow { try { emit(1) emit(2) println("This line will not execute") emit(3) } finally { println("Finally in numbers") }}@ExperimentalCoroutinesApifun main() = runBlocking<Unit> { numbers() .take(2) // take only the first two .collect { value -> println(value) }}
![Page 166: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/166.jpg)
Size-limiting Operatorsfun numbers(): Flow<Int> = flow { try { emit(1) emit(2) println("This line will not execute") emit(3) } finally { println("Finally in numbers") }}@ExperimentalCoroutinesApifun main() = runBlocking<Unit> { numbers() .take(2) // take only the first two .collect { value -> println(value) }}
Output:
1 2 Finally in numbers
![Page 167: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/167.jpg)
Terminal Flow Operators
@ExperimentalCoroutinesApifun main() = runBlocking<Unit> { val sum = (1..5).asFlow() .map { it * it } // squares of numbers from 1 to 5 .reduce { a, b -> a + b } // sum them (terminal operator) println(sum)}
![Page 168: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/168.jpg)
Terminal Flow Operators
@ExperimentalCoroutinesApifun main() = runBlocking<Unit> { val sum = (1..5).asFlow() .map { it * it } // squares of numbers from 1 to 5 .reduce { a, b -> a + b } // sum them (terminal operator) println(sum)}
Output: 55
![Page 169: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/169.jpg)
Terminal Flow Operators
@ExperimentalCoroutinesApifun main() = runBlocking<Unit> { val sum = (1..5).asFlow() .map { it * it } // squares of numbers from 1 to 5 .reduce { a, b -> a + b } // sum them (terminal operator) println(sum)}
Output: 55
kotlinlang.org/docs/reference/coroutines/flow.html
![Page 170: Lecture #7 Reactive Programmingdan/ma/Lecture7.pdfWhy Reactive? • Unless you can model your entire system synchronously, a single asynchronous source breaks imperative programming](https://reader031.vdocuments.us/reader031/viewer/2022040903/5e7572e1607e5211296a7714/html5/thumbnails/170.jpg)
Lecture outcomes
• Understand the reactive programming (rx) concepts.
• Use rx to re-write the application logic.
• Design a real time application logic against a real time backend.
• Understand coroutines and flow.