Where to place sqlite3_open code for ios - ios

On first view that is displayed to the user, I have to populate some text fields with some saved information on the phone.
Does apple have a diagram for the entire state of app-launchign sequence? Ie, that includes the app delegate method calls and the view delegate method calls? It seems that order of events switch for different iOS versions...
For example:
http://oleb.net/blog/2011/06/app-launch-sequence-ios/
Lets say you had a custom view controller in MainWindow.xib, won't viewWillAppear be called before didFinishLaunchingWithOptions?
To be safe, should I just wrap a quick call around all my sqlite3 database functions that check if the database is open before continuing, and open it if necessary?

I used an SQL Database in an iPhone application that put opened the database if it was closed inside a void called from viewDidLoad. It wasn't detrimental at all to the performance of the application.
Though in a newer app I needed to grab some preferences (not in an SQL Database this time) as the application opened so I created an instance of my ViewController and called it's reload preferences method from the App Delegate's -(void)applicationDidBecomeActive:(UIApplication *)application; method (Reruns the method every time my app becomes active) like so:
- (void)applicationDidBecomeActive:(UIApplication *)application{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
ViewController *instanceOfVC = [[[ViewController alloc] init]autorelease];
[instanceOfVC checkPreferences];
}

Related

save state when phone turned off

I created a saveState() method that uses UserDefaults to save certain settings at certain parts of my App. It works fine when I exit the App and return, but if I actually turn my (iOS) phone off, when I start the App again, the settings are not saved. In addition to those places where I call the saveState() method in the code, I also call saveState() in three AppDelegate functions: applicationWillResignActive, applicationDidEnterBackground and applicationWillTerminate. I have a loadState() function in viewDidLoad so any saved information will load at that time. Does anyone know what I am not doing re: saving/restoring settings when phone powered off?
When iphone off, AppDelegate's applicationWillTerminate
function Called when the application is about to terminate
how about use synchronize() after your saveState() called
In apple API https://developer.apple.com/reference/foundation/userdefaults/1414005-synchronize
Writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk.

Are Today Extension Ivars Reset Constantly?

I'm writing code to a new iOS 8 Today widget, but I noticed that each time that widgetPerformUpdateWithCompletionHandler: is called my ivars (created from #property) are reset. It's is like every time a new view controller is getting instantiated.
This makes it impossible to save data on memory between updates to the widget (while it is in the background, for example, and is called to update its content).
Is this normal behaviour, or a bug? Should I save my simple numbers to NSUserDefaults instead of relying on memory based data, which is being reset?
Your extension will not be running in between calls to widgetPerformUpdateWithCompletionHandler:. That method is called when iOS launches your extension in the background for you to fetch new data. The OS then captures an image of your extension (thats what the completion handler is for) to show as a sort of "launch screen" for your extension (when notification center is launched your extension isn't available immediately so it shows the image until it is). You likely want to use NSUserDefaults (or another method) to store cached data to load while waiting for updated data to come from a server.
In other words, the OS will launch your app periodically to let you fetch new data so that the user will always see updated data in notification center. You should cache this data in that method so that you can load your extension faster when it is launched for notification center. This is all discussed here.

IOS when to make server call at app launch time

There are a few ways your app can launch:
User opens app
Push notification leads to opening of app.
What is the recommended design to know when to make a async network call to your server to fetch some data? For example, didFinishLaunchingWithOptions gets called at app launcht ime, so would it make sense to put that call in that function? What about putting the call in viewDidLoad of my root view controller? viewDidLoad only gets called once for load while viewWillAppear gets called too frequently.
What's a good pattern or design to handle both cases of app launch.

Where do I put logic for when an iOS application hard closes and comes back?

I have an iOS application where I need to persist a set of data after the application HARD CLOSES (when the user double clicks the Home button and slides the application upwards). Then, when the application comes back to the foreground, I need to fetch that data and do something with it. I'm just not sure where I need to put that logic for Application Hard Close and Resuming the Application.
In your AppDelegate
When your app is going to be closed, but still in Multitasking menu the following method is getting called
-(void)applicationWillResignActive:(UIApplication*)application
If after 3 minutes user doesn re-open your app this method is going to be called
-(void)applicationDidEnterBackground:(UIApplication*)application
If user re-opens your app from multitasking menu the following method is getting called
-(void)applicationWillEnterForeground:(UIApplication*)application
If user is going to close your app from multitasking menu this method is getting called(you will have limited time to perform some logic here)
-(void)applicationWillTerminate:(UIApplication*)application
When user presses home twice applicationWillResignActive and applicationDidEnterBackground is called. You can save data here.
When user opens app, applicationWillEnterForeground is called, you get data which you save and process.
When a user hard-closes the application, the UIViewController's Delegate's method called applicationWillTerminate. Here I can catch and save the model data before it's all destroyed.
Then when a user launches the application again, there are many choices like didFinishLaunchingWithOptions where I can grab the data stored to disk.
Your app no longer gets an applicationWillTerminate call ever. You are simply silently killed while in the background. You have to write your app to save state in the applicationDidEnterBackground handler, as nmh describes. That's your only option.

applicationWillTerminate: not being called

I'm using applicationWillTerminate: to save some last-minute stuff. But the problem is that it never gets called. If I do something like this at the top of the method: NSLog(#"Something"); it doesn't get called and doesn't get outputted to the console.
Does anyone know why this is happening?
From Apple docs:
For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. 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.
If your app has background enabled use:
- (void)applicationDidEnterBackground:(UIApplication *)application
Use applicationDidEnterBackground: instead. Applications aren't terminated when you press the home button in a multitasking system.
From the iOS Application Programming Guide on Core Application Design and Application life time:
The applicationWillTerminate: method is not called if your application is currently suspended.
If you are linking against iOS 4.0, you should also save data in applicationDidEnterBackground:.
step 1: command + shift + h (double click(tab))
step 2: move app top side (kill)
step 3: applicationWillTerminate Work

Resources