This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WorkManager.getInstance() | |
.beginUniqueWork(Constants.UNIQUE_NAME, ExistingWorkPolicy.REPLACE, task1) | |
.then(task2) | |
.then(task3) | |
.enqueue() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UUID syncWorkId = syncOnlyOnceWork.getId(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add initial delay only for OneTimeWorkRequest | |
val syncOnlyOnce = OneTimeWorkRequestBuilder<SyncWorker>() | |
.setInitialDelay(15, TimeUnit.MINUTES) | |
.build() | |
// backoff delay and policy | |
val syncOnlyOnce = OneTimeWorkRequestBuilder<SyncWorker>() | |
.setBackoffCriteria(BackoffPolicy.LINEAR, | |
OneTimeWorkRequest.MIN_BACKOFF_MILLIS, | |
TimeUnit.MICROSECONDS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val leftChain = WorkManager.getInstance() | |
.beginWith(task1) | |
.then(task2) | |
val rightChain = WorkManager.getInstance() | |
.beginWith(task3) | |
.then(task4) | |
val resultChain = WorkContinuation | |
.combine(listOf(leftChain, rightChain)) | |
.then(task5) | |
resultChain.enqueue() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// immediate assignment | |
val countOfEvenNumbers: Int = 10 | |
// `Int` type is inferred | |
var sumOfNumbers = 0 | |
// type required when no initializer is provided | |
var countOfPrimeNumbers: Int | |
// deferred assignment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin Numeric Types Examples | |
val myByte: Byte = 10 | |
val myShort: Short = 125 | |
val myInt = 3000 | |
val myLong = 3000L // The suffix 'L' is used to specify a long value | |
val myFloat = 123.45f // The suffix 'f' or 'F' represents a Float | |
val myDouble = 321.05 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numbers = arrayOf(1, 2, 3, 4, 5) | |
var colors = arrayOf("red", "blue", "pink", "yellow") | |
// primitive arrays | |
val myCharArray = charArrayOf('K', 'O', 'T', 'L', 'I', 'N') // CharArray (corresponds to Java 'char[]') | |
val myIntArray = intArrayOf(1, 3, 5, 7, 9, 11) // IntArray (corresponds to Java 'int[]') | |
// Array constructor | |
val numbersArray = Array(8, { i -> i * 2 }) // 0 2 4 6 8 10 12 14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val firstWord = "Learn " | |
val secondWord = "Kotlin" | |
var bothWords = "$firstWord $secondWord" | |
println("$bothWords has ${bothWords.length}") | |
println(""""$bothWords" has ${bothWords.length}""") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val number = 0 | |
val result = if (number > 0) { | |
"Positive number" | |
} else if (number < 0) { | |
"Negative number" | |
} else { | |
"Zero" | |
} | |
println(result) // => Zero |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val firstValue = 6 | |
val secondValue = 3 | |
val operator = "/" | |
val resultOfOperation = when (operator) { | |
"+" -> firstValue + secondValue | |
"-" -> firstValue - secondValue | |
"*" -> firstValue * secondValue | |
"/" -> firstValue / secondValue | |
else -> "$operator operator is invalid." | |
} |