possible problem with removing location listener on iOS

I’m writing an app that starts by finding the user location, then it fetches the closest weather report and displays it. Once I get the location, I don’t need the app to keep updating its location, so I call Runtime:removeEventListener( “location”, locationHandler ).

However, this seems to not work on iOS (not tested on Android yet). The location keeps updating after the removeEventListener command was called. For now I worked around it by setting a flag that tells the app to ignore any future location events.

Anyone experiencing the same problem? [import]uid: 33608 topic_id: 22037 reply_id: 322037[/import]

Anyone? [import]uid: 33608 topic_id: 22037 reply_id: 90333[/import]

This isn’t an issue I’ve run into and I often remove Runtime listeners.

If you can provide a project to reproduce you can file a bug report on this although please be certain you’ve debugged the code first. [import]uid: 52491 topic_id: 22037 reply_id: 90425[/import]

@ amirfl7 Have you resolved this issue? I seem to be seeing the same issue with iPad using daily build 2012.766. I have tested with the Corona Sample Code using the GPS app and get the same results as my own application that I am building. Maybe I’m doing something wrong here and hopefully someone can point out where and what I am doing that can fix this, but I modified the sample code by inserting a single line into the location handler:

local locationHandler = function( event )  
 -- Check for error (user may have turned off Location Services)  
 if event.errorCode then  
 native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} )  
 print( "Location error: " .. tostring( event.errorMessage ) )  
 else  
 -- inserted line is HERE  
 Runtime:removeEventListener("location", locationHandler)  
 -- inserted line is HERE  
  
 local latitudeText = string.format( '%.4f', event.latitude )  
 currentLatitude = latitudeText  
 latitude:setText( latitudeText )  
  
 local longitudeText = string.format( '%.4f', event.longitude )  
 currentLongitude = longitudeText  
 longitude:setText( longitudeText )  
  
 local altitudeText = string.format( '%.3f', event.altitude )  
 altitude:setText( altitudeText )  
  
 local accuracyText = string.format( '%.3f', event.accuracy )  
 accuracy:setText( accuracyText )  
  
 local speedText = string.format( '%.3f', event.speed )  
 speed:setText( speedText )  
  
 local directionText = string.format( '%.3f', event.direction )  
 direction:setText( directionText )  
  
 -- Note: event.time is a Unix-style timestamp, expressed in seconds since Jan. 1, 1970  
 local timeText = string.format( '%.0f', event.time )  
 time:setText( timeText )  
 end  
end  

It seems when I build this for iPad and run it that the event listener is not removed as I can see that the time input field is being updated every second. [import]uid: 36336 topic_id: 22037 reply_id: 94180[/import]

I was very busy with other tasks this week, so I just worked around the problem by not removing the Runtime listener at all. Probably not the best thing for battery life, but it’s also not that bad for now.
Peach, sorry I didn’t have time to create a test program, for now the work around is working well. Maybe tomorrow I’ll have some time to try it again. [import]uid: 33608 topic_id: 22037 reply_id: 94189[/import]

OK. I found a solution:

http://www.ludicroussoftware.com/blog/2011/08/24/remove-all-listeners/

This worked for me, but still not sure if this is what I need to be doing to stop the location events or if there is something I am missing with the Corona API. Any other input would be appreciated. [import]uid: 36336 topic_id: 22037 reply_id: 94203[/import]

OK. So now there might be an issue using the previous posted workaround as adding the listener back in doesn’t seem to be working. Anybody else having this issue and have a better workaround? [import]uid: 36336 topic_id: 22037 reply_id: 94845[/import]

Ok, I took a few minutes to write a test program.

There is a text object showing the current location latitude. To make it easier to see the event firing, without checking the console, I made the text object change color every time the location event fires.

With the Corona sim, the event fires only ONCE, no matter if the removeEventListener is called or not.

With the iOS sim, or the actual device, on the other hand, the events KEEP firing repeatedly regardless of removeEventListener.

[code]
local txt = display.newText("Lat: ",0,0,native.systemFont, 18)
txt.x,txt.y = display.contentWidth/2,display.contentHeight/2;

local locationHandler = function( event )

– Check for error (user may have turned off Location Services)
if event.errorCode then
native.showAlert( “GPS Location Error”, event.errorMessage, {“OK”} )
print( "Location error: " … tostring( event.errorMessage ) )
else
print("Lat: ",event.latitude);
local color = {};
color[1] = math.random(255)
color[2] = math.random(255)
color[3] = math.random(255)

txt.text = "Lat: "…event.latitude;
txt:setTextColor(color[1],color[2],color[3]);

–I want the event to fire only once, then remove the event listener
–testing on the iOS simulator and the actual iPhone device, the event never gets removed
print(“Removing the event listener so it fires only once”);
Runtime:removeEventListener( “location”, locationHandler );
end
end

– Activate location listener
Runtime:addEventListener( “location”, locationHandler );
[/code] [import]uid: 33608 topic_id: 22037 reply_id: 94862[/import]

Hey Amir, have you filed this as a bug with your sample? If not please do!

Peach :slight_smile: [import]uid: 52491 topic_id: 22037 reply_id: 95037[/import]

Just submitted [import]uid: 33608 topic_id: 22037 reply_id: 95156[/import]

Is this bug still happening?

Is this bug still happening?

I’m still getting this on all device.

Since this is a really old thread, can I get you to file a bug report with a simple app (complete with build.settings, config.lua and main.lua) that demonstrates the issue and use the Report a Bug feature above.  You will get an email with the bug # in it.  Please post it back here as a reference.

Thanks

Rob

I’m still getting this on all device.

Since this is a really old thread, can I get you to file a bug report with a simple app (complete with build.settings, config.lua and main.lua) that demonstrates the issue and use the Report a Bug feature above.  You will get an email with the bug # in it.  Please post it back here as a reference.

Thanks

Rob