Data myData = new Data.Builder() .putInt(KEY_ONE_INT, aInt) .putIntArray(KEY_ONE_INT_ARRAY, aIntArray) .putString(KEY_ONE_STRING, aString) .build();
inline fun workDataOf(vararg pairs: Pair<String, Any?>): Data
val data = workDataOf( KEY_MY_INT to myIntVar, KEY_MY_INT_ARRAY to myIntArray, KEY_MY_STRING to myString )
class MyWork(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
return try { // Do something Result.success() } catch (error: Throwable) { Result.failure() } } }
Diversamente da Worker, questo codice non viene eseguito sull'Executor specificato nella configurazione di WorkManager.
override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
Raramente è necessario cambiare il Dispatcher che CoroutineWorker sta usando e Dispatchers.Default è una buona opzione per la maggior parte dei casi.
@RunWith(JUnit4::class) class MyWorkTest { private lateinit var context: Context
@Before fun setup() { context = ApplicationProvider.getApplicationContext() }
@Test fun testMyWork() { // Get the ListenableWorker val worker = TestListenableWorkerBuilder<MyWork>(context).build()
// Run the worker synchronously val result = worker.startWork().get()
assertThat(result, `is`(Result.success())) } }
override suspend fun doWork(): Result { val serverUrl = inputData.getString("SERVER_URL")
return try { // Do something with the URL Result.success() } catch (error: TitleRefreshError) { if (runAttemptCount <3) { Result.retry() } else { Result.failure() } } } }
@Test fun testMyWorkRetry() { val data = workDataOf("SERVER_URL" to "http://fake.url")
// Get the ListenableWorker with a RunAttemptCount of 2 val worker = TestListenableWorkerBuilder<MyWork>(context) .setInputData(data) .setRunAttemptCount(2) .build()
// Start the work synchronously val result = worker.startWork().get()
assertThat(result, `is`(Result.retry())) }
@Test fun testMyWorkFailure() { val data = workDataOf("SERVER_URL" to "http://fake.url")
// Get the ListenableWorker with a RunAttemptCount of 3 val worker = TestListenableWorkerBuilder<MyWork>(context) .setInputData(data) .setRunAttemptCount(3) .build()
assertThat(result, `is`(Result.failure())) }
tokenizer = tensorflow_text.WhitespaceTokenizer() tokens = tokenizer.tokenize(['everything not saved will be lost.', u'Sad☹'.encode('UTF-8')]) print(tokens.to_list())
[['everything', 'not', 'saved', 'will', 'be', 'lost.'], ['Sad\xe2\x98\xb9']]
tokenizer = tensorflow_text.UnicodeScriptTokenizer() tokens = tokenizer.tokenize(['everything not saved will be lost.', u'Sad☹'.encode('UTF-8')]) print(tokens.to_list())
[['everything', 'not', 'saved', 'will', 'be', 'lost', '.'], ['Sad', '\xe2\x98\xb9']]
def _CreateTable(vocab, num_oov=1): init = tf.lookup.KeyValueTensorInitializer( vocab, tf.range(tf.size(vocab, out_type=tf.int64), dtype=tf.int64), key_dtype=tf.string, value_dtype=tf.int64) return tf.lookup.StaticVocabularyTable( init, num_oov, lookup_key_dtype=tf.string) vocab_table = _CreateTable(["great", "they", "the", "##'", "##re", "##est"]) tokens = [["they're", "the", "greatest"]] tokenizer = tensorflow_text.WordpieceTokenizer( vocab_table, token_out_type=tf.string) result = tokenizer.tokenize(tokens) print(result.to_list())
[[['they', "##'", '##re'], ['the'], ['great', '##est']]]
tokenizer = tensorflow_text.UnicodeScriptTokenizer() (tokens, offset_starts, offset_limits) = tokenizer.tokenize_with_offsets(['everything not saved will be lost.', u'Sad☹'.encode('UTF-8')]) print(tokens.to_list()) print(offset_starts.to_list()) print(offset_limits.to_list())
[['everything', 'not', 'saved', 'will', 'be', 'lost', '.'], ['Sad', '\xe2\x98\xb9']] [[0, 11, 15, 21, 26, 29, 33], [0, 3]] [[10, 14, 20, 25, 28, 33, 34], [3, 6]]
androidx-packaged
com.google.android.gms
com.google.firebase
androidx
gradle.properties
android.useAndroidX=true android.enableJetifier=true
MaterialApp( theme: ThemeData( brightness: Brightness.light, primaryColor: Colors.red, ), );
MaterialApp( theme: ThemeData( brightness: Brightness.light, primaryColor: Colors.red, ), darkTheme: ThemeData( brightness: Brightness.dark, ), );