Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Contributor
sebastian_wr
Posts: 8
Registered: ‎10-09-2013
My Device: Developer
My Carrier: none
Accepted Solution

QSignalMapper invokes slot multiple times

Hi,

 

I get a list of about 20 objects in JSON format. For every object, I need to send another request to receive more information. So I've been doing this:

 

	for (int i = 0; i < entries.count(); i++) {
QVariantMap entry = entries.value(i).toMap();
QNetworkRequest request = QNetworkRequest(); QString url = QString("..."); request.setUrl(url); QNetworkAccessManager* nam = new QNetworkAccessManager(); QNetworkReply* reply = nam->get(request); bool result = connect(reply, SIGNAL(finished()), this, SLOT(onRequestFinished())); Q_ASSERT(result); }

 This works, but unfortunately, due to the asynchronous nature, I lose track of what entry the reply is for. So I added a signal mapper to match reply with the ID of an entry:

 

	for (int i = 0; i < entries.count(); i++) {

		QVariantMap entry = entries.value(i).toMap();

		QNetworkRequest request = QNetworkRequest();
		QString url = QString("...");

		request.setUrl(url);
		qDebug() << "Geocoding address from URL: " << url;

		QNetworkAccessManager* nam = new QNetworkAccessManager();
		QNetworkReply* reply = nam->get(request);

		bool result = connect(reply, SIGNAL(finished()), signalMapper,
				SLOT(map()));
		Q_ASSERT(result);

		QString entryId = entry.value("id").toString();

		signalMapper->setMapping(reply, entryId);

		result = connect(signalMapper, SIGNAL(mapped(QString)), this,
				SLOT(onRequestFinished(QString)));
		Q_ASSERT(result);
	}

 This would also be a good approach, but now the "enhanced" onRequestFinished signal is invoked multiple times (20 or 30 times). Why is this happening and what can I do about it?

Please use plain text.
Developer
BBSJdev
Posts: 5,948
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QSignalMapper invokes slot multiple times

You've put you network request inside a loop, unless I've missed something multiple responses is exactly what you are asking for?


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
New Contributor
sebastian_wr
Posts: 8
Registered: ‎10-09-2013
My Device: Developer
My Carrier: none

Re: QSignalMapper invokes slot multiple times

Basically, yes. But I get multiple responses for each loop iteration. That's not what I want. :smileywink:

Please use plain text.
Developer
BBSJdev
Posts: 5,948
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QSignalMapper invokes slot multiple times

I take it you've checked that your entries aren't being duplicated in some fashion?


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
BBSJdev
Posts: 5,948
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QSignalMapper invokes slot multiple times

...and that whatever you are requesting doesn't naturally send multiple packets?

 

It looks like some kind of geolocation request so if it's a moving target perhaps?

 

The code looks okay to me which is why I'm reaching for problems. :-)

 

You could always take the first one coming in and set a flag within your mapper saying ignore any further signals.

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
New Contributor
sebastian_wr
Posts: 8
Registered: ‎10-09-2013
My Device: Developer
My Carrier: none

Re: QSignalMapper invokes slot multiple times

Yes, I definitely get more signal invokations than entries in the array. I also connected the finished signal without the SignalMapper in between and get one singal invokation per request.

 

I now added a QMap<QString, bool> to see, whether I already got a response for the entry ID. So although, the finished() signal is invoked multiple times, I only care about this, if the flag in the QMap is still false.

Please use plain text.
Developer
BBSJdev
Posts: 5,948
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QSignalMapper invokes slot multiple times

Just as a sanity check I would do a hard-wired request, commenting out the loop, send one request then see if multiples come back.

 

If it does I would look further up the logic tree to see if it's anything up there, for example if you start this by linking in to an onTouch signal then multiple touch signals will be fired at the point the user hits the control so multiple function calls will be made to your request function.

 

I'm not saying this is it but It could be something like this.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
BBSJdev
Posts: 5,948
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QSignalMapper invokes slot multiple times

You replied while I wrote the last response so it can't be anything like that then. Strange.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
BBSJdev
Posts: 5,948
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QSignalMapper invokes slot multiple times

How have you defined the SignalMapper object and post any code relating to that?


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
BBSJdev
Posts: 5,948
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: QSignalMapper invokes slot multiple times

The id is definately unique right, you've checked that?


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.