- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我目前正在使用 Anko 的 doAsync 来处理异步任务。具体来说,我使用它来解析对数据对象的 ArrayList 的响应 json,如下所示:
// Asynchronously parse server response
doAsync {
val itemlist = parseResponse(response)
uiThread {
Otto.INSTANCE.post(UpdateRedeemedVoucherViewsEvent(itemlist))
}
}
大多数时候,就像在 Google Pixel (Android 8.0.0) 上一样,它工作得很好。我的对象得到解析,Otto 总线事件 将发布到 ui 线程。
但在极少数情况下,此过程会失败。到目前为止,我注意到 ui 线程调用不会在 Galaxy S5 Mini (Android 5.1.1) 上触发。虽然我的数据解析没有任何错误或异常,但 uiThread 括号中的代码 fragment 不会被执行。
到目前为止,我无法确定此行为的任何原因。没有 logcat 日志,什么都没有。没有关于何时会出现此错误的方案。
有人对 kotlin 和/或 Anko 有相同或类似的问题吗?
更新:我自己找到了解决方案,请在下面查看我的回答以获得解释。
最佳答案
谢谢大家的帮助。我自己弄明白了,所以我发布了这个答案,以防万一其他人遇到这个问题。
TL;DR:context 丢失,因此 uiThread 不会执行
解决方法:我偶然发现了这个 post其中提到了 uiThread 方法的一个重要特征:
Basically you have an async function that will execute it’s code in another thread, and will give the chance of returning main thread using uiThread. async is an extension function implemented for Context, and will use a weak reference of it, so it won’t prevent GC from releasing its memory.
经过进一步调查,我在解析完成后发现了以下日志,并且应该调用 uiThread 的位置:
I/art: Background partial concurrent mark sweep GC freed 30879(16MB) AllocSpace objects, 16(2MB) LOS objects, 40% free, 16MB/27MB, paused 2.123ms total 196.372ms
所以看起来确实有一些 GC 操作,这可能会或可能不会释放 context。
为了防止这种情况,我使用了 Ankos runOnUiThread,它可以在给定的上下文引用中调用:
// Asynchronously parse server response
doAsync {
val itemlist = parseResponse(response)
context.runOnUiThread {
Otto.INSTANCE.post(UpdateRedeemedVoucherViewsEvent(itemlist))
}
}
据我所知,这似乎已经成功了。
关于android - Anko 的 uiThread 偶尔不会被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46795795/
我有 4 个 TextView。它们一开始都是不可见的,我将它们动画到屏幕上(从底部滑入)。我启动一个线程来为每个动画制作动画,每个动画开始时间间隔 200 毫秒。问题是:开始 Activity 后第
我有一个奇怪的问题,我似乎无法解决。 :(我有一个基于 Web 的应用程序,可以发送电子邮件。它通过连接在本地网络上设置的基于 Windows 的 SMTP 服务器来实现。此 SMTP 服务器不需要我
从昨天开始,我一直遇到一些设备收不到推送通知的问题。证书/设备 token 似乎是正确的,因为直到昨天,该设备还可以成功接收推送通知。 在服务器端,没有错误或连接拒绝,而且推送通知似乎每次都发送成功。
我有一个将 Angular 错误记录到数据库的应用程序,并且我偶尔会看到此错误的日志 Error: [$compile:tpload] http://errors.angularjs.org/1.5.
我收到一个 ImportError from azure.storage.blob import BlobServiceClient 但是我意识到当我安装依赖项时会发生错误 pip install a
我已经完成了使用AVAudioPlayer实现的简单音频播放GUI。 播放声音时,我使用UISlider提供播放反馈... 这是奇怪的地方。 我有一个非常偶尔发生的问题-而且大多数情况都不会发生,因此
在我的 XNA 游戏中,我的计算机(2.5 Ghz 双核 Intel Centrino 2)在 Debug模式下有一个大约需要 10 毫秒的操作。它只会每隔几分钟运行一次,它基本上只是整数数学/按位运
我相信我的应用程序编码正确(至少大部分是这样),因为它在大约 98% 的时间里都能正常工作。但是,大约 2% 的时间,我得到的似乎是有效的身份验证 token ,但是当我去发布照片时,我收到以下错误“
我在 apple store 上有一个应用程序,在 iOS6 更新后,我在 MKMapView 中收到了数百份崩溃报告。我无法在我的设备上重现崩溃。它看起来像是 EAGLContext 的问题。我们不
我最近开始使用 Foundation 5(我第一次使用 Foundation),我遇到了一个问题,似乎任何元素都响应了 .click 事件(例如顶部导航栏,单击元素时显示的数据下拉列表)将随机不起作用
花了大约一整天的时间试图解决这个问题,希望有人能帮助我!我是新来的,所以如果这是一个重复的问题,我深表歉意(我做了我的研究,但无法提前找到任何东西)。 我正在为我的咨询公司构建的网站使用浏览器缓存,但
我有一个表格,使用了以下 CSS 和 HTML: .price { position:relative; display:block; text-align:center; }
我们使用 ansible 将多个节点配置为一个集群。这些机器是在自定义 AWS 类似基础设施上创建的实例。 我们在不同的剧本上有大约一百个任务,它们在每个节点上执行。 问题是,我们收到零星的主机无法访
我们偶尔会从用户那里收到崩溃报告(我们使用崩溃报告程序包将崩溃发送到我们的服务器),并出现以下错误: “非法尝试在不同上下文中的对象之间建立关系‘...’” (不同的报表可能有不同的关系,并非所有报表
我是一名优秀的程序员,十分优秀!