loadPage().next(bundle).execute() 将导致无限循环



我正在尝试获取分页捆绑包中的所有条目。

工作代码如下:

Bundle bundle = fhirClient.search()
.forResource(...)
.where(...)
.returnBundle(Bundle.class)
.execute();
if (bundle == null){
return null;
}
// Keep adding entries until the last page
Bundle tempBundle = fhirClient.loadPage().next(bundle).execute();
// Put all the new entries into a separated list so that we do not modify the iterator
List<Bundle.Entry> entries = new ArrayList<>();
while(tempBundle != null){
entries.addAll(tempBundle.getEntry());
if(tempBundle.getLink(Bundle.LINK_NEXT) != null) {
tempBundle = fhirClient.loadPage().next(tempBundle).execute();
}
else{
tempBundle = null;
}
}
entries.forEach(bundle::addEntry);
return bundle;

但是,我在while循环中做了这样的事情,它最终会无限循环:

while(tempBundle != null){
entries.addAll(tempBundle.getEntry());
tempBundle = fhirClient.loadPage().next(tempBundle).execute();
}

我预计 tempBundle 最终会以 null 结束,因为没有下一页的捆绑包,但这永远不会发生。知道吗?

你可能想看看这个例子: 捆绑提取器.java

尤其是使用以下页面填充结果的部分:

while (bundle.getLink(IBaseBundle.LINK_NEXT) != null) {
bundle = client
.loadPage()
.next(bundle)
.execute();
patients.addAll(BundleUtil.toListOfResources(ctx, bundle));
}

我认为您的代码中的违规部分是

// Keep adding entries until the last page
Bundle tempBundle = fhirClient.loadPage().next(bundle).execute();

最新更新