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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
New Contributor
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?

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
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. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
New Contributor
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. Smiley Wink

Highlighted
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
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. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
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. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
New Contributor
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.

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
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. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
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. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
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. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
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. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.