diff --git a/examples/cpp/dds/LifespanQoSExample/LifespanSubscriber.cpp b/examples/cpp/dds/LifespanQoSExample/LifespanSubscriber.cpp index 737f92b99f2..edc3c991272 100644 --- a/examples/cpp/dds/LifespanQoSExample/LifespanSubscriber.cpp +++ b/examples/cpp/dds/LifespanQoSExample/LifespanSubscriber.cpp @@ -155,18 +155,32 @@ void LifespanSubscriber::run( std::cout << std::endl << "Subscriber waiting for " << sleep_ms << " milliseconds" << std::endl << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms)); - LifespanPubSubType::type data; - SampleInfo info; + LoanableSequence data_values; + SampleInfoSeq sample_infos; + + int32_t max_samples = listener.n_samples; + ReturnCode_t result = reader_->take( + data_values, + sample_infos, + max_samples, + ANY_SAMPLE_STATE); - for ( uint32_t i = 0; i < listener.n_samples; i++ ) + if (result == ReturnCode_t::RETCODE_OK) { - if (reader_->take_next_sample((void*) &data, &info) == ReturnCode_t::RETCODE_OK) + size_t samples_taken = sample_infos.length(); + for (size_t i = 0; i < samples_taken; ++i) { - std::cout << "Message " << data.message() << " " << data.index() << " read from history" << std::endl; - } - else - { - std::cout << "Could not read message " << i << " from history" << std::endl; + Lifespan& sample = data_values[i]; + SampleInfo& sample_info = sample_infos[i]; + + std::cout << "Message " << sample.message() << " with index " << sample.index() + << " read from history." << std::endl; } } + else + { + std::cout << "Could not read message from history" << std::endl; + } + + reader_->return_loan(data_values, sample_infos); }