Where to open/close SQLite3 is better?

Discussion in 'iOS Programming' started by mikezang, Oct 2, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    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
     
  2. roel03 macrumors newbie

    Joined:
    Oct 2, 2010
    #2
    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];
    }
     
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    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?
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    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?
     
  6. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #6
    I need to read and update a lot of times, I think it is not good for open or close frequently...
     

Share This Page