开发一款需要一次性向同一个端点发送多个 API 调用的应用。
例如 - 目录浏览场景,需要通过为当前文件夹中的所有文件夹发送 get 调用来获取目录结构。问题是,正确改造中的所有文件夹的响应分别出现,但 LiveData observable 只给我整个列表的一个响应。
目录结构:-
test -> temp -> temp1 -> temp2
-> temp3
-> temp4
Observable 监听回调:-
mViewModel.getServerFilesLiveData().observe(this, browseServerDataResource -> {
if (browseServerDataResource != null) {
if (browseServerDataResource.status == APIClientStatus.Status.SUCCESS) {
if (browseServerDataResource.data != null) {
Timber.i("Got data for path %s in Observable", browseServerDataResource.data.path);
if (browseServerDataResource.data.folderList != null
&& browseServerDataResource.data.folderList.size() > 0) {
for (final String name : browseServerDataResource.data.folderList) {
final ServerDirectoryPathInfo pathInfo = new ServerDirectoryPathInfo();
pathInfo.completePath = browseServerDataResource.data.path + "/" + name;
getFolderDownloadPath(pathInfo.completePath);
}
}
mFolderCountToParse--;
Timber.d("Folders left to parse %d", mFolderCountToParse);
if (mFolderCountToParse == 0) {
showToast("Parsed all folders");
}
}
}
}
});
调用以获取数据的函数:-
private void getFolderDownloadPath(@NonNull final String path) {
mViewModel.getServerFiles(path);
mFolderCountToParse++;
}
对服务器的改造调用:-
public LiveData<Resource<BrowseServerData>> getServerFiles(@NonNull final String additionalUrl) {
final MutableLiveData<Resource<BrowseServerData>> data = new MutableLiveData<>();
final String url = mMySharedPreferences.getCurrentUrl()
+ AppConstants.DIRECTORY_END_POINT
+ AppConstants.PATH_END_POINT
+ (TextUtils.isEmpty(additionalUrl) ? "" : additionalUrl);
Timber.i("Requesting data for - api %s", url);
mAPI.getServerFiles(url, mMySharedPreferences.getNetworkName())
.enqueue(new Callback<BrowseServerData>() {
@Override
public void onResponse(
@NonNull Call<BrowseServerData> call, @NonNull Response<BrowseServerData> response
) {
if (response.body() != null && response.isSuccessful()) {
if (!TextUtils.isEmpty(response.body().path)) {
Timber.i("Got response for = %s in Retrofit", response.body().path);
}
data.setValue(
new Resource<>(APIClientStatus.Status.SUCCESS, response.body(), null, null));
} else {
ErrorMessage errorMessage = null;
try {
errorMessage = Utility.getApiError(response, mRetrofit);
} catch (IOException e) {
e.printStackTrace();
}
if (errorMessage != null) {
data.setValue(
new Resource<>(APIClientStatus.Status.ERROR, null, errorMessage.message(), call));
} else {
data.setValue(
new Resource<>(APIClientStatus.Status.ERROR, null, response.message(), call));
}
}
}
@Override
public void onFailure(@NonNull Call<BrowseServerData> call, @NonNull Throwable throwable) {
data.setValue(
new Resource<>(APIClientStatus.Status.ERROR, null, throwable.getMessage(), throwable,
call));
}
});
return data;
}
数据如下:-
I: Got response for = ./test in Retrofit
I: Got data for path ./test in Observable
I: Got response for = ./test/temp in Retrofit
I: Got data for path ./test/temp in Observable
I: Got response for = ./test/temp/temp1 in Retrofit
I: Got data for path ./test/temp/temp1 in Observable
I: Got response for = ./test/temp/temp1/temp2 in Retrofit
I: Got response for = ./test/temp/temp1/temp4 in Retrofit
I: Got response for = ./test/temp/temp1/temp3 in Retrofit
I: Got data for path ./test/temp/temp1/temp3 in Observable
如您所见,Observable 中的数据仅来自一个文件夹 temp3。
在调用电话时添加随机延迟后,数据会正常发送:-
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getFolderDownloadPath(pathInfo.completePath);
}
}, new Random().nextInt(10000 - 1000) + 1000);
现在至少有 3 个文件夹中的 2 个有数据:-
I: Got response for = . in Retrofit
I: Got data for path . in Observable
I: Got data for the current directory, don't need it, skipping
I: Got response for = ./test in Retrofit
I: Got data for path ./test in Observable
I: Got response for = ./test/temp in Retrofit
I: Got data for path ./test/temp in Observable
I: Got response for = ./test/temp/temp1 in Retrofit
I: Got data for path ./test/temp/temp1 in Observable
I: Got response for = ./test/temp/temp1/temp3 in Retrofit
I: Got response for = ./test/temp/temp1/temp2 in Retrofit
I: Got data for path ./test/temp/temp1/temp2 in Observable
I: Got response for = ./test/temp/temp1/temp4 in Retrofit
I: Got data for path ./test/temp/temp1/temp4 in Observable
知道为什么会发生这种情况以及解决方法吗?
更新:- 添加有助于调用服务器的 ViewModel 构造函数
@Inject
BrowseHubMediaViewModel(@NonNull Application application, @NonNull APIClient mAPIClient) {
super(application);
mGetServerFilesMutable = new MutableLiveData<>();
mGetServerFilesLiveData =
Transformations.switchMap(mGetServerFilesMutable, mAPIClient::getServerFiles);
}
从 ViewModel 获取 Observable
/**
* Observer to listen for file listing in server
*
* @return LiveData<Resource<BrowseServerData>>
*/
public LiveData<Resource<BrowseServerData>> getServerFilesLiveData() {
return mGetServerFilesLiveData;
}
最佳答案
switchmap 丢弃所有以前的项目,只接受最新的项目。
我:在 Retrofit 中得到了对 = ./test/temp/temp1/temp2 的响应
我:在 Retrofit 中得到了对 = ./test/temp/temp1/temp4 的响应
我:在 Retrofit 中得到了对 = ./test/temp/temp1/temp3 的响应
I: 在 Observable 中获取路径 ./test/temp/temp1/temp3 的数据
您按顺序调用了 temp2 temp4 和 temp3,当 temp2 和 temp4 的数据出现时您正在调用 temp3。所以 temp2 和 temp3 的 Observable 将被丢弃,只有 temp4 的 Observable 将被返回。
我认为这可能会解决您的问题。您可以阅读有关 switchMap 的更多信息。这样就更清楚了。
关于如果快速调用,android LiveData Observable 不会返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48375816/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳