Skip to content

Commit c59df79

Browse files
committedJun 14, 2018
Send request from spawned thread
1 parent 9b5c5b0 commit c59df79

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed
 

‎src/providers/wfs/qgswfsrequest.cpp

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -128,41 +128,46 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
128128
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
129129
}
130130

131-
mReply = QgsNetworkAccessManager::instance()->get( request );
132-
mReply->setReadBufferSize( READ_BUFFER_SIZE_HINT );
133-
if ( !mUri.auth().setAuthorizationReply( mReply ) )
131+
std::function<bool()> downloaderFunction = [ this, request, synchronous ]()
134132
{
135-
mErrorCode = QgsWfsRequest::NetworkError;
136-
mErrorMessage = errorMessageFailedAuth();
137-
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
138-
return false;
139-
}
140-
connect( mReply, &QNetworkReply::finished, this, &QgsWfsRequest::replyFinished );
141-
connect( mReply, &QNetworkReply::downloadProgress, this, &QgsWfsRequest::replyProgress );
142-
143-
if ( !synchronous )
144-
return true;
145-
else
146-
{
147-
QEventLoop loop;
148-
connect( this, &QgsWfsRequest::downloadFinished, &loop, &QEventLoop::quit );
149-
150-
if ( QThread::currentThread() == QApplication::instance()->thread() )
133+
bool success = true;
134+
mReply = QgsNetworkAccessManager::instance()->get( request );
135+
mReply->setReadBufferSize( READ_BUFFER_SIZE_HINT );
136+
if ( !mUri.auth().setAuthorizationReply( mReply ) )
151137
{
152-
QFuture<void> future = QtConcurrent::run( [ &loop ]()
153-
{
154-
loop.exec();
155-
} );
156-
157-
future.waitForFinished();
138+
mErrorCode = QgsWfsRequest::NetworkError;
139+
mErrorMessage = errorMessageFailedAuth();
140+
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
141+
success = false;
158142
}
159143
else
160144
{
161-
loop.exec();
145+
connect( mReply, &QNetworkReply::finished, this, &QgsWfsRequest::replyFinished );
146+
connect( mReply, &QNetworkReply::downloadProgress, this, &QgsWfsRequest::replyProgress );
147+
148+
if ( synchronous )
149+
{
150+
QEventLoop loop;
151+
connect( this, &QgsWfsRequest::downloadFinished, &loop, &QEventLoop::quit );
152+
loop.exec();
153+
}
162154
}
155+
return success;
156+
};
157+
158+
bool success;
163159

164-
return mErrorMessage.isEmpty();
160+
if ( synchronous && QThread::currentThread() == QApplication::instance()->thread() )
161+
{
162+
QFuture<bool> future = QtConcurrent::run( downloaderFunction );
163+
future.waitForFinished();
164+
success = future.result();
165+
}
166+
else
167+
{
168+
success = downloaderFunction();
165169
}
170+
return success && mErrorMessage.isEmpty();
166171
}
167172

168173
bool QgsWfsRequest::sendPOST( const QUrl &url, const QString &contentTypeHeader, const QByteArray &data )

0 commit comments

Comments
 (0)
Please sign in to comment.