Does the locationManager:didEnterRegion works when stopUpdatingLocation is called - objective-c

I am wondering does the CLLocationManager delegates like didEnterRegion and didExitRegion work when stopUpdatingLocation is called or the the method startUpdatingLocation must called all the time to make them called ?
Another thing is where the best place to start regions monitoring

If your app enters the background you are expected to call stopUpdatingLocation to preserve energy. If you use startMonitoringForRegion:desiredAccuracy: your delegate methods didEnterRegion and didExitRegion will be called accordingly, even if your app is in background/suspended. Another option is to use startMonitoringSignificantLocationChanges. Then your app will be woken up from suspended or terminated status.

Related

Order of NSApplication delegate calls

I'm noticing something strange in my NSApplication delegate callbacks. When I start the app with debugger attached, I see what I expect: applicationDidFinishLaunching: is called first, then applicationDidBecomeActive:
When I run the app without the debugger, I get the calls in the order reversed: applicationDidBecomeActive: is called before applicationDidFinishLaunching:
Is there a reason for this? It makes it very confusing to account for different scenarios based on debugger vs. non-debugger.
[note: testing this is in Mavericks]
The relative order of those delegate methods during launch is not documented, so you should not rely on any particular order.
If you're concerned about some initialization not having been done when -applicationDidBecomeActive: is called, then you should do that initialization in -applicationWillFinishLaunching: rather than in -applicationDidFinishLaunching:. Alternatively, you should do the initialization on demand, such as initializing a property when its value is first requested.

ios - how to cause a delay in the middle of a method

I'm working on a card game and trying to get cards to deal one after the other. I have a method that animated a card from the deck to a player, and in viewDidLoad I call this method four times. The problem is all four cards get dealt simultaneously. How do I stop a method in its tracks for a period of time?
I know that the scheduledTimerWithTimeInterval method calls another method after a delay, but I'm looking for a way to interrupt the current method after calling the deal method once and then continuing with the rest of the current method. sleep() also doesn't work. I tried putting it between calls to the deal method, but it just executed all the sleep()s and then did all the animation at once again. Any help is much appreciated. Thanks!
You are going down the wrong road. Attempting to sleep a method is not the way to approach this. You want to break the task into steps to be performed serially and perform each step only after the previous step is completed.
Say you have a variable called 'cardCounter' and one called 'cardMax'. Then you have a method called 'dealCard'. In viewDidAppear you intialize 'cardCounter` to zero and 'cardMax' to 4 (or however many cards are to be dealt. Then you call the 'dealCard' method.
(actually, you probably want a method called newGame or something since you will likely want to have multiple games and you don't want to tie your game setup to the viewDidAppear event. So in viewDidAppear you would call 'newGame' and do your initialization there.)
- (void)dealCard {
cardCounter++;
if (cardCounter > cardMax){
// all cards are dealt
// call some method to start game
// or do any other set up;
} else {
// call some method to animate the card
// using core animation with a completion handler?
// using a ^block with a completion handler?
// either way, in the completion handler call
// 'dealCard' again
}
A hint. Create a setup method which is called from your init only once! In that setup method you can work with `performSelector" as tomi said. The Selector is "your" method that moves a card from the deck to the player.
(void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

Interrupted method when user pushes home button?

What happens when a user pushes the home button on the iOS device and the app is currently running a method: Will the method finish running or will the method be interrupted in the middle?
Like Maudicus wrote, applicationWillResignActive: is called on the application delegate, as well as applicationDidEnterBackground:. You should assume that any method that is currently running will be interrupted. If you want to ensure that a particular operation is performed, you should put it in one of the aforementioned methods.

use applicationDidBecomeActive to call viewDidLoad

I want to make sure that all my initializations for my views and stuff are handled every time my application starts, even when it is called back after being sent to the background, such as with multitasking.
What's the best way to do this? should i use applicationDidBecomeActive to call viewDidLoad on my viewcontroller directly? I'm guessing this is not wise. I just want to make sure that stuff gets done on load every time the user calls up the app, no matter what state it is in at the time.
I have several apps published that do just that - call viewDidLoad on one or several UIViewControllers from applicationDidBecomeActive.
It works just fine. Apple doesn't object to it either.
However, be aware that if you have allocations in your viewDidLoad you need to either check for already allocated instances or release the instances you allocate in viewDidLoad when your app suspends. The same goes for DB connections that need to be closed, notification listeners, and so on.
As long as you watch for these elements and handle them correctly, the approach is valid and very usable.

- (void)applicationWillTerminate:(UIApplication *)application

I found this method in a delegate file and inserted some code. The code never gets executed, even when the home button is pressed. When or how does this function get called?
You should use applicationDidEnterBackground method if your app and OS support multitasking.
From applicationWillTerminate docs:
For applications that support background
execution, this method is generally
not called when the user quits the
application because the application
simply moves to the background in that
case. However, this method may be
called in situations where the
application is running in the
background (not suspended) and the
system needs to terminate it for some
reason.