1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <QCoroNetworkReply>
QCoro::Task<> MyClass::fetchData() {
// Creates QNetworkAccessManager on stack
QNetworkAccessManager nam;
// Calls QNetworkAccessManager::get() and co_awaits on the returned QNetworkReply*
// until it finishes. The current coroutine is suspended until that.
auto *reply = co_await nam.get(QUrl{QStringLiteral("https://.../api/fetch")});
// When the reply finishes, the coroutine is resumed and we can access the reply content.
const auto data = reply->readAll();
// Raise your hand if you ever forgot to delete a QNetworkReply...
reply->deleteLater();
doSomethingWithData(data);
// Extra bonus: the QNetworkAccessManager is destroyed automatically, since it's on stack.
}
|