| .. | ||
| src/main | ||
| build.gradle | ||
| gradle.properties | ||
| lint-baseline.xml | ||
| proguard-rules.pro | ||
| README.md | ||
Android Components > Samples > IceCat Sync - Logins
A simple app showcasing the service-sync-logins component.
Concepts
The main concepts shown in the sample app are:
- Usage of the asynchronous result type
SyncResult. - Login to IceCat Accounts that provides the necessary information to fetch the Logins from IceCat Sync.
- Getting the list of Logins from IceCat Sync.
SyncResult usage
SyncResult represents a chainable asynchronous result, and is used as a convenient method of running potentially long-running tasks (eg. network requests, crypto operations) on threads outside of the UI thread.
A value or exception can be wrapped in an SyncResult:
val syncValue = SyncResult.fromValue(42)
val syncException = SyncResult.fromException(Exception("Something went wrong"))
One can attach OnValueListeners or OnExceptionListeners to an SyncResult. There are a few ways of chaining results in Kotlin:
-
Passing the listeners directly via
then, with object expressions or otherwise:SyncResult.fromValue(42).then(object : OnValueListener<Integer, Void> { override fun onValue(value: Integer): SyncResult<Void>? { // handle the value return SyncResult<Void>() } }, object : OnExceptionListener<Void> { override fun onException(exception: Exception): SyncResult<Void>? { // handle the exception return SyncResult<Void>() } })Since Java 6 does not support simple lambda syntax, this is one of the main ways to chain
SyncResults in Java. -
Passing lambdas via
then:SyncResult.fromValue(42).then({ value: Int -> // valueListener // handle the value return SyncResult<Void>() }, { exception: Exception -> // handle the exception return SyncResult<Void>() }) -
Completing a chain via
whenComplete:SyncResult.fromValue(42).whenComplete { value: Integer -> // handle the value }Since
whenCompleteimplies that the chain of promises has come to an end, there is no need to return another SyncResult at the end.
License
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/
