From cb2f09a8e3673e420970d1d2c0f5c1688a0e1fbf Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 11 Oct 2018 20:07:20 +0200 Subject: Geo lookup: make the QNetworkAccessManager a static object According to Qt's docs, the QNetworkAccessManager is supposed to be a long-living object. Therefore, don't create one on every geo-lookup, but a single object for all geo-lookups. By making the object function-local it is only initiaized on first use. Morover this limits the amount of concurrent geo lookups. Signed-off-by: Berthold Stoeger --- core/divesitehelpers.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/divesitehelpers.cpp b/core/divesitehelpers.cpp index b77bc8fec..7a7176624 100644 --- a/core/divesitehelpers.cpp +++ b/core/divesitehelpers.cpp @@ -22,8 +22,11 @@ void reverseGeoLookup() { + // By making the QNetworkAccessManager static and local to this function, + // only one manager exists for all geo-lookups and it is only initialized + // on first call to this function. + static QNetworkAccessManager rgl; QNetworkRequest request; - QNetworkAccessManager *rgl = new QNetworkAccessManager(); QEventLoop loop; QString geonamesURL("http://api.geonames.org/findNearbyPlaceNameJSON?lang=%1&lat=%2&lng=%3&radius=50&username=dirkhh"); QString geonamesOceanURL("http://api.geonames.org/oceanJSON?lang=%1&lat=%2&lng=%3&radius=50&username=dirkhh"); @@ -38,7 +41,7 @@ void reverseGeoLookup() // first check the findNearbyPlaces API from geonames - that should give us country, state, city request.setUrl(geonamesURL.arg(uiLanguage(NULL).section(QRegExp("[-_ ]"), 0, 0)).arg(ds->latitude.udeg / 1000000.0).arg(ds->longitude.udeg / 1000000.0)); - QNetworkReply *reply = rgl->get(request); + QNetworkReply *reply = rgl.get(request); timer.setSingleShot(true); QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); timer.start(5000); // 5 secs. timeout @@ -114,7 +117,7 @@ void reverseGeoLookup() } // next check the oceans API to figure out the body of water request.setUrl(geonamesOceanURL.arg(uiLanguage(NULL).section(QRegExp("[-_ ]"), 0, 0)).arg(ds->latitude.udeg / 1000000.0).arg(ds->longitude.udeg / 1000000.0)); - reply = rgl->get(request); + reply = rgl.get(request); QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); timer.start(5000); // 5 secs. timeout loop.exec(); @@ -161,6 +164,4 @@ void reverseGeoLookup() clear_reply: reply->deleteLater(); - - rgl->deleteLater(); } -- cgit v1.2.3-70-g09d2