- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想获取 API 请求并将请求的数据保存到数据库中。还想返回数据(即写入数据库)。我知道,这在 RxJava 中是可能的,但现在我在 Kotlin 协程中编写,目前使用 Fuel 而不是 Retrofit(但差异不是很大)。我读了How to use Fuel with a Kotlin coroutine ,但不明白。
如何编写协程和方法?
更新
比如说,我们有一个 Java 和 Retrofit,RxJava。然后我们就可以写代码了。
区域响应:
@AutoValue
public abstract class RegionResponse {
@SerializedName("id")
public abstract Integer id;
@SerializedName("name")
public abstract String name;
@SerializedName("countryId")
public abstract Integer countryId();
public static RegionResponse create(int id, String name, int countryId) {
....
}
...
}
地区:
data class Region(
val id: Int,
val name: String,
val countryId: Int)
网络:
public Single<List<RegionResponse>> getRegions() {
return api.getRegions();
// @GET("/regions")
// Single<List<RegionResponse>> getRegions();
}
区域库:
fun getRegion(countryId: Int): Single<Region> {
val dbSource = db.getRegion(countryId)
val lazyApiSource = Single.defer { api.regions }
.flattenAsFlowable { it }
.map { apiMapper.map(it) }
.toList()
.doOnSuccess { db.updateRegions(it) }
.flattenAsFlowable { it }
.filter({ it.countryId == countryId })
.singleOrError()
return dbSource
.map { dbMapper.map(it) }
.switchIfEmpty(lazyApiSource)
}
区域交互器:
class RegionInteractor(
private val repo: RegionRepository,
private val prefsRepository: PrefsRepository) {
fun getRegion(): Single<Region> {
return Single.fromCallable { prefsRepository.countryId }
.flatMap { repo.getRegion(it) }
.subscribeOn(Schedulers.io())
}
}
最佳答案
我们一层一层的看。
首先,据我所知,您的 RegionResponse
和 Region
完全适合这个用例,所以我们根本不会碰它们。
您的网络层是用 Java 编写的,因此我们假设它始终需要同步行为,并且也不会触及它。
所以,我们从代码库开始:
fun getRegion(countryId: Int) = async {
val regionFromDb = db.getRegion(countryId)
if (regionFromDb == null) {
return apiMapper.map(api.regions).
filter({ it.countryId == countryId }).
first().
also {
db.updateRegions(it)
}
}
return dbMapper.map(regionFromDb)
}
请记住,我没有您的代码,因此细节可能会有所不同。但是协程的一般想法是,你用 async()
启动它们,以防它们需要返回结果,然后编写你的代码,就好像你在一个完美的世界里,而你不需要需要关心自己的并发性。
现在是交互者:
class RegionInteractor(
private val repo: RegionRepository,
private val prefsRepository: PrefsRepository) {
fun getRegion() = withContext(Schedulers.io().asCoroutineDispatcher()) {
val countryId = prefsRepository.countryId
return repo.getRegion(countryId).await()
}
}
您需要一些东西来将异步代码转换回同步代码。为此,您需要某种线程池来执行。这里我们使用 Rx 的线程池,但如果你想使用其他池,也可以。
关于android - 如何在 Kotlin 中将 Fuel 与协程一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52145644/
在以下问题上,我需要您的帮助:无法 mock Fuel.get call 。 我尝试过的 这是服务类,将在其中调用Fuel。 class JiraService { private val l
问题 我想通过使用 Fuel 作为 Httpclient 对 API 进行同步调用来获得 get 请求的结果。我在 Android (Anko) 项目中使用 Fuel。该调用只是一个简单的获取请求,它
一直以来,对于openstack 的初学者来讲,安装往往是入门的头大难题。在e版本之前,要搭建一个基本能用的openstack 环境那是相当麻烦,自己要装机,自己搞源,自己照着文档敲命令,又没有靠谱
我有这个代码: $(window).load(function() { var DataSource = function (options)
我尝试使用复选框选择所有内容,但无法实现它。 2 all $(document).ready(function() { $('
我的 Fuel CMS/Codeigniter 安装中有一个名为“tradeusers”的简单模块。这会从数据库中的 tradeusers 表中列出站点上的贸易用户。 这意味着我的客户将能够从 CMS
我正在尝试使用燃料 ux 向导组件,如示例所示: http://exacttarget.github.io/fuelux/#wizard 但它不能正确显示。 我的代码是:
我正在使用新发布的 Fuel UX 3.1在网络应用程序中。我设法使用来自后端的数据设置了一个 Repeater 小部件,但是我只能渲染它一次。 $('#myRepeater').repeater({
我正在使用此示例将数据导入 Fuel UX 数据网格:http://dailyjs.com/2012/10/29/fuel-ux/ (显然使用我自己的 API 而不是 Flickr) 我在示例代码中看
根据documentation ,我应该能够 Hook Fuel UX 向导组件的更改事件。但对于我的生活,我无法弄清楚如何。我还是 jquery 和 javascript 的新手。 Javascri
我正在尝试将网站从 apache2 站点移动到 Azure 托管站点。我将 Azure 配置为 PHP 网站。我将网站的内容复制到 Azure 网站中。 我安装了 Pear PHP 不知道为什么,但有
我在使用 Fuel 的 responseObject 时遇到问题以一般的方式。我正在尝试开发一种集中式方法,其中组件的 HTTP 响应对象已经反序列化,准备就绪。它看起来像这样: class Cont
我使用名为“Fuel”的库从服务器获取数据,这是我的代码: button1.setOnClickListener { val jb = "{......}"
我正在开发 Fuel Framework,我需要使用 order_by title 构建查询,该查询是 varchar 但其中大多数都是数字。我需要按真正的数字顺序对其进行排序。 所以我想使用“CAS
所以我在我的网站中实现了 Fuel UX 的 TreeView 。每当它加载时,我都需要手动重新选择我想要的项目。是否可以在每次重新加载后预选某些项目? 提前致谢! 最佳答案 我从昨天开始就处于同样的
我正在尝试在一个项目中使用 Fuel UI Spinner,该项目使用 Bower 进行依赖项管理,使用 gulp.js 构建文件。 这是我的代码: gulpfile.js: ... gulp.ta
下面是我的fuelphp项目的目录结构 燃料 日志 公开 tmp 在我的公共(public)文件夹中 Assets index.php 当我点击基本 URL 以外的任何 URL 时,它会抛出以下错误
我需要向服务器发送 POST 请求。我应该传递一些参数和图像。我可以通过 Postman 执行此操作,但我无法使用 Fuel 在我的 Android 应用程序(最新的 SDK)上执行此操作。 这是我正
在 Android 应用程序中,我尝试使用 Fuel在 Kotlin 协程中发出 HTTP 请求。我的第一个尝试是在这样的包装器中使用同步模式: launch(UI) { val token
我想获取 API 请求并将请求的数据保存到数据库中。还想返回数据(即写入数据库)。我知道,这在 RxJava 中是可能的,但现在我在 Kotlin 协程中编写,目前使用 Fuel 而不是 Retrof
我是一名优秀的程序员,十分优秀!