问题
I have created small app that use this project as it's core:
https://github.com/dsdavids/TTLocationHandler
And it worked fine until I have moved stating location services from another view in app.
What app is doing: When it is started you can tap on START button and (in emulator locations must be enabled) on the map route of movement is displayed as you move.
The problem came when I moved starting action in a second view.
In that second view I just want to start location service and close it.
The problem is when I start locating on the second view I get error (application crash EXC_BAD
) here:
TTLocationHandler
...
dispatch_async(dispatch_get_main_queue(), ^{
if (OUTPUT_LOGS) NSLog(@"Sending notification out");
NSNotification *aNotification = [NSNotification notificationWithName:LocationHandlerDidUpdateLocation object:[locationToSave copy]];
[[NSNotificationCenter defaultCenter] postNotification:aNotification];
});
...
I think that it is because I close the second view (view that started service) and TTLocationHandler
still tries to send it something.
For better understanding my problem I have added project at git hub:
https://github.com/1110/common-location-features
You can download it and run start service from second view and when close that view app will crash in a few seconds.
I would be really thankful if someone can find a little time to tell me what am I doing wrong here as I am sure that it is some small thing that I probably doing wrong.
Whole code is in SecondViewController.m
Thanks
回答1:
The problem is probably that the location manager is sending location updates to an object that no longer exists.
I don't have the time to dig through all of your code, but generally speaking once you tell the location manager to startUpdatingLocation
, you need to keep your location manager delegate object around until you tell it to stop. If what you refer to as the "second view" is your delegate object, then you can't let that view get deallocated until you tell the location manager to stopUpdatingLocation
.
Generally you want to have one object be the CLLocationManagerDelegate
, and keep that object around for as long as you need it. The delegate will get notified every time the location changes, and it is the delegate's responsibility to update any views that care about the location.
来源:https://stackoverflow.com/questions/23940557/when-close-view-that-started-locations-service-notification-error-occure