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

  • Home
  • SEARCH
  • 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 511567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:13:47+00:00 2026-05-13T07:13:47+00:00

In a cocoa app of mine, there is a method that does some operations

  • 0

In a cocoa app of mine, there is a method that does some operations on a sqlite database’s schema that could be destructive, so first we make a rollback copy of the database, and if there’s an error, we call the following method to replace the db file with the rollback file. Note that I definitely want the db file replaced with the rollback!

- (BOOL)restoreDatabaseFromFileAtPath:(NSString *)backupPath error:(NSError **)error {
    NSFileManager *fm = [NSFileManager defaultManager];
    // get the db paths
    NSString *databasePath = [sharedManager pathToDatabase];

    // insist that the two files be present
    NSAssert1([fm fileExistsAtPath:databasePath], @"no db file at %@", databasePath);
    NSAssert1([fm fileExistsAtPath:backupPath], @"no backup db file at %@", backupPath);

    // remove the original to make way for the backup
    NSLog(@"removing the file at the primary database path...");
    if ([fm removeItemAtPath:databasePath error:error]) {
        // now move the backup to the original location
        NSLog(@"moving the backup file into the primary database path...");
        if ([fm moveItemAtPath:backupPath toPath:databasePath error:error]) {
            return YES;
        }
    }
    [self presentError:error]; // at this point we're in real trouble
    return NO;
}

The code shown does work, but it’s potentially very destructive and not exactly atomic. What I would really like to do is something that I imagine exists, but I can’t find a way to do it in the API for NSFileManager, something like:

Bool success = [fm moveFileAtPath:backupPath toPath:databasePath overwriteDestination:YESPLZ error:&error];

The method that comes closest to this barfs when it discovers there’s already a file at the destination path.

Small update based on responses so far:

There’s got to be some standard way this has traditionally been done by Cocoa devs, to atomically replace a file. The various ‘move’ methods of NSFileManager don’t provide a facility for this out of the box. Also, looking to support 10.5 and up in this lib as well as iPhone OS 3+.

  • 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-13T07:13:47+00:00Added an answer on May 13, 2026 at 7:13 am

    The C-level FSMoveObjectSync/FSMoveObjectAsync functions might help. These functions take a set of options, which can include a flag to overwrite the destination, kFSFileOperationOverwrite, among other things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just starting out on my first 'real' cocoa app. Note that this is
My Cocoa app needs some small dynamically generated windows. How can I programmatically create
I have a Cocoa app that uses a WebView to display an HTML interface.
I have a Cocoa app I'm writing that has an ImageView with a TextView
I have a cocoa app with two types windows each of which requires a
I'm adding repeating events to a Cocoa app I'm working on. I have repeat
I'm hacking on a simple Cocoa app to make blocks move around the screen
The situation I'm trying to solve: in my Cocoa app, I need to encrypt
Interface Builder can be used for basic dependency injection in a Cocoa app, but
I'm working on a Cocoa Mac app where I need to display a window/view

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.