Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7516081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:01:16+00:00 2026-05-30T01:01:16+00:00

I saw this post which was helpful in copying a .db file I include

  • 0

I saw this post which was helpful in copying a .db file I include with my app and copy it to the appropriate directory: Where would you place your SQLite database file in an iPhone app?

Does the method that does the work provide any type of failure or error handling? Like if the user thought the app was stuck b/c the transfer was taking too long and decided to force quit the app?

Method:

// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success)
        return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:01:18+00:00Added an answer on May 30, 2026 at 1:01 am

    No, It doesn’t do anything if there’s an existing file with the same name.

    So if the copying was interrupted for some reason in the middle, then next time it runs it’ll think the file is already there.

    Probably you should make the copying with a temporary file first and then if it finishes, rename it to the right name.

    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    
    // Getting the writable path of the file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];
    
    // Checking if the file exists at the writable path
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success)
        return;
    
    // Incase the file does not exist
    // Getting the temporary path of the file
    NSString *tempDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb_temp.sql"];
    
    // Just making sure no temporary file exists from a previous operation
    success = [fileManager fileExistsAtPath:tempDBPath];
    if (success)
    {
        success=[fileManager removeItemAtPath:tempDBPath error:&error];
        NSAssert1(success, @"Failed to delete temp database file with message '%@'.", [error localizedDescription]);
    }
    
    // Getting the default path of the file
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"];
    
    // Copying the file from the default path to the temporary
    NSLog(@"Starting to copy");
    success = [fileManager copyItemAtPath:defaultDBPath toPath:tempDBPath error:&error];
    NSAssert1(success, @"Failed to copy database file with message '%@'.", [error localizedDescription]);
    NSLog(@"Finished copying");
    
    // Renaming the temporary file which wouldn't take time but to ensure the copying has finished successfully
    success= [fileManager moveItemAtPath:tempDBPath toPath:writableDBPath error:&error];
    NSAssert1(success, @"Failed to rename temp database file with message '%@'.", [error localizedDescription]); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this post on Sitepoint quoting a statement by Rasmus Lerdorf which goes
On this awesome forum I saw a post which shows how to convert a
I saw some code, html form code which looks like this: <form name=form1 method=post
I saw this post: Typos… Just use option strict and explicit please.. during one
I saw this question asked about C# I would like an answer for PHP.
I saw this post Ruby on Rails - Awesome nested set plugin but I
I saw this code snippet which talks about how to group socket connections from
i am confused by the concept of on-demand loading I saw this blog post
I've run into problems installing the latest Subclipse plug in. I saw this post:
I saw a post on this already, but it didn't really provide a solution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.