loadPage().next(bundle).execute() 会导致死循环

loadPage().next(bundle).execute() would lead to infinite loop

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

工作代码如下:

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 结束,但它永远不会发生。有什么想法吗?

您可能想看看这个例子: BundleFetcher.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();