kotlin

How to spot where “Job was cancelled” exception comes from when all your coroutines are already wrapped with a CouroutineExceptionHandler?

六眼飞鱼酱① 提交于 2021-02-08 21:57:49
问题 I read all the kotlinx UI docs and implement a ScopedActivity like described there (see the code below). In my ScopedActivity implementation, I also add a CouroutineExceptionHandler and despite that I pass my exception handler to all my coroutines, my users are experiencing crashes and the only info I get in the stacktrace is "Job was cancelled". I searched for a couple of days now but I did not find a solution and my users are still randomly crashing but I do not understand why... Here is my

How do I convert a Firestore date/Timestamp to Date in Kotlin?

拜拜、爱过 提交于 2021-02-08 21:48:10
问题 i'm working with firestore, and getting stuck when i get the timestamp from firestore. how i convert this Timestamp(seconds=1556006384, nanoseconds=994000000) to Date in kotlin. my minimum SDK is 21 so the code below doesn't work val timestamp = it["time"] as com.google.firebase.Timestamp val milliseconds = timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000 val tz = ZoneId.of("Asia/Jakarta (UTC+07:00)") val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), tz

How to upload a file using Ktor client

流过昼夜 提交于 2021-02-08 20:52:21
问题 I have read the docs about HTTP requests in Ktor clients, but it lacks of an example of file upload. It mentions PartData.FileItem , but it's unclear how to use it. So, how do I prepare a multipart/form-data request for file upload in Ktor? 回答1: You should use submitFormWithBinaryData's formData parameter to provide a list of parts. There is a helper function with the same name to create such list. HttpClient(Apache).use { client -> val parts: List<PartData> = formData { // Regular form

How to upload a file using Ktor client

☆樱花仙子☆ 提交于 2021-02-08 20:51:23
问题 I have read the docs about HTTP requests in Ktor clients, but it lacks of an example of file upload. It mentions PartData.FileItem , but it's unclear how to use it. So, how do I prepare a multipart/form-data request for file upload in Ktor? 回答1: You should use submitFormWithBinaryData's formData parameter to provide a list of parts. There is a helper function with the same name to create such list. HttpClient(Apache).use { client -> val parts: List<PartData> = formData { // Regular form

Optional Observables in combineLatest

余生长醉 提交于 2021-02-08 19:43:35
问题 As definied combineLatest in rx emites only if all values have changed at least once. (so long as each of the source Observables has emitted at least one item) I use it to manipulate views within my android views. For example, I enable a call to action button as soon as all observables emitted a valid value. Otherwise I disable it. Observable.combineLatest( toObservable(email), toObservable(username), toObservable(status), toObservable(phonenumber), toObservable(birthdate), Function5<String,

Optional Observables in combineLatest

偶尔善良 提交于 2021-02-08 19:43:14
问题 As definied combineLatest in rx emites only if all values have changed at least once. (so long as each of the source Observables has emitted at least one item) I use it to manipulate views within my android views. For example, I enable a call to action button as soon as all observables emitted a valid value. Otherwise I disable it. Observable.combineLatest( toObservable(email), toObservable(username), toObservable(status), toObservable(phonenumber), toObservable(birthdate), Function5<String,

Kotlin “also” function strange warning in Android Studio

十年热恋 提交于 2021-02-08 15:43:13
问题 There is two presumably identical snippets: // Verbose version val typedArray = context.obtainStyledAttributes(attrs, styleable) block(typedArray) typedArray.recycle() // One-line version context.obtainStyledAttributes(attrs, styleable).also(block).recycle() I'm wonder why verbose block of code looks fine to Android Studio, whereas one-line version highlights obtainStyledAttributes and gives following warning: This TypedArray should be recycled after use with #recycle() Does anyone know is it

MediaRecorder Android 11 start failed -1004

我们两清 提交于 2021-02-08 15:18:36
问题 On Android 11 my MediaRecorder fails to initialize. I suspect the problem is related to scopedstorage, but I have been unable to figure out the cause. I am using MediaRecorder to record audio from the microphone. I extract the amplitude from the audio, so I have no intention to keep the file, that is why the path is /dev/null var mRecorder: MediaRecorder? = null if (mRecorder == null) { mRecorder = MediaRecorder() mRecorder!!.setAudioSource(MediaRecorder.AudioSource.MIC) mRecorder!!

Kotlin super.finalize()

假装没事ソ 提交于 2021-02-08 15:16:20
问题 While migration to Kotlin from Java I faced with a problem. I overrided Object 's finalize() method: @Override protected void finalize() throws Throwable { stopTimer(); super.finalize(); } When I tried to do the same with Kotlin I found to solutions. The first one is from the doc: protected fun finalize() { stopTimer() super.finalize() } And the second one from the article (it's in Russian): @Suppress("ProtectedInFinal", "Unused") protected fun finalize() { stopTimer() super.finalize() } But

Kotlin super.finalize()

守給你的承諾、 提交于 2021-02-08 15:16:07
问题 While migration to Kotlin from Java I faced with a problem. I overrided Object 's finalize() method: @Override protected void finalize() throws Throwable { stopTimer(); super.finalize(); } When I tried to do the same with Kotlin I found to solutions. The first one is from the doc: protected fun finalize() { stopTimer() super.finalize() } And the second one from the article (it's in Russian): @Suppress("ProtectedInFinal", "Unused") protected fun finalize() { stopTimer() super.finalize() } But