Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikezang

macrumors 6502a
Original poster
May 22, 2010
939
41
Tokyo, Japan
I am using SQLite3 to store data in my Tab bar application, I want to know where is better place to open and close DB.

1. open in didFinishLaunchingWithOptions and close in dealloc of app

2. open viewDidLoad and close in viewDidUnLoad of every view
 
I use the applicationDidFinishLaunching method as shown below.


Code:
(void)applicationDidFinishLaunching:(UIApplication *)application {
	cities = [[NSMutableArray alloc] init];
	NSString *filePath = [self copyDatabaseToDocuments];
	[self readCitiesFromDatabaseWithPath:filePath];
	
	navController.viewControllers = [NSArray arrayWithObject:viewController];
	[window addSubview:navController.view];
	[window makeKeyAndVisible];
}
 
I use the applicationDidFinishLaunching method as shown below.


Code:
(void)applicationDidFinishLaunching:(UIApplication *)application {
	cities = [[NSMutableArray alloc] init];
	NSString *filePath = [self copyDatabaseToDocuments];
	[self readCitiesFromDatabaseWithPath:filePath];
	
	navController.viewControllers = [NSArray arrayWithObject:viewController];
	[window addSubview:navController.view];
	[window makeKeyAndVisible];
}
So you open it and use it, then close it.
I thought that I need open it until app finished or view unload, is it not good?
 
Your app delegate's dealloc method will likely never be called. Instead you should look over the app lifecycle methods that are called when the app will terminate and when it goes into the background.

I would usually think of this kind of thing though in terms of a view controller's life cycle.

Remember that viewDidUnload may never be called.
 
So you open it and use it, then close it.
How are you able to tell that they are opening and closing based solely on the code posted?

P.S. You should keep the database open for as long as you need to access it. If you read from it once, at the app startup, why wouldn't you close it right away?
 
How are you able to tell that they are opening and closing based solely on the code posted?

P.S. You should keep the database open for as long as you need to access it. If you read from it once, at the app startup, why wouldn't you close it right away?
I need to read and update a lot of times, I think it is not good for open or close frequently...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.