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 6852407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:22:00+00:00 2026-05-27T01:22:00+00:00

I am having our app move it’s readonly db to the App Support directory

  • 0

I am having our app move it’s readonly db to the App Support directory so that content updates can update it as well(over the wire updates, not app updates). The code below is in the app delegate to copy over the db, however on the first run NSFileManager in following attempts(during that run) to see if it is there or to load it does not see the copied file. However, it is copying it because if I close the app and restart it, everything works fine. I’m at a lose.

NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];
NSError *err = nil;
NSURL *ASD =  [fm URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&err];

if (!err) {
    NSURL* path = [NSURL URLWithString:DATABASE_NAME relativeToURL:ASD];
    NSString *bundle =  [[ NSBundle mainBundle] pathForResource:@"datafiles/data_main" ofType:@"sqlite"];

    if ([fm fileExistsAtPath:[path path]]) {
        if([DatabaseManager isBundledDBNewerThenInUse]){
            NSLog(@"bundled db is newer");
            [DatabaseManager close];
            [fm removeItemAtURL:path error:&err];
            if (err) {
                NSLog(@"Error deleting old DB:%@",err);
            }
            else {
                [fm copyItemAtPath:bundle toPath:[path path] error:&err];
                if (err) {
                    NSLog(@"Error in copying over DB:%@",err);
                }
                else
                {
                    NSLog(@"db should have been copied over correctly");
                }
            }
        }
    }
    else{
        [fm copyItemAtPath:bundle toPath:[path path] error:&err];
        if (err) {
            NSLog(@"Error in copying over DB:%@",err);
        }
    }
}
else
{
    NSLog(@"Error in opening AS for DB copy:%@",err);
}

“[DatabaseManager isBundledDBNewerThenInUse]” returns YES if the db in the App Support directory either isn’t there or has a version that is older then the one in the bundle. It opens the db in the App Support directory thus the [DatabaseManager close] before trying to remove it. I’m using FMDB in my DatabaseManager if that helps at all. But as I said after that initial load if you kill the app and go back into it it work perfectly. (On a database update from bundle you the db isn’t updated on that first load either.) Any help would be great and if you need more info please just ask! Thanks!

  • 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-27T01:22:01+00:00Added an answer on May 27, 2026 at 1:22 am

    The answer was dispatch_async on the main thread and then double checking the cached data and reloading it if needed. Corrected code:

    dispatch_async(dispatch_get_main_queue(),^(){
        NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];
        NSError *err = nil;
        NSURL *ASD =  [fm URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&err];
    
        if (!err) {
            NSURL* path = [NSURL URLWithString:DATABASE_NAME relativeToURL:ASD];
            NSString *bundle =  [[ NSBundle mainBundle] pathForResource:@"datafiles/data_main" ofType:@"sqlite"];
    
            if ([fm fileExistsAtPath:[path path]]) {
                if([DatabaseManager isBundledDBNewerThenInUse]){
                    NSLog(@"bundled db is newer");
                    [DatabaseManager close];
                    [fm removeItemAtURL:path error:&err];
                    if (err) {
                        NSLog(@"Error deleting old DB:%@",err);
                    }
                    else {
                        [fm copyItemAtPath:bundle toPath:[path path] error:&err];
                        if (err) {
                            NSLog(@"Error in copying over DB:%@",err);
                        }
                        else
                        {
                            NSLog(@"db should have been copied over correctly");
                        }
                    }
                }
            }
            else{
                [fm copyItemAtPath:bundle toPath:[path path] error:&err];
                if (err) {
                    NSLog(@"Error in copying over DB:%@",err);
                }
                else
                    NSLog(@"DB Copied");
            }
        }
        else
        {
            NSLog(@"Error in opening AS for DB copy:%@",err);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem at work in that our app which uses WCF for
I currently have an iOS app that allows people to submit content to our
We unfortunately find ourselves having to support our product in IE 6 because some
As usual, I'm having strange issues with deploying a Rails app to our different
I'm experimenting with using cagradientlayer to draw gradients in our app instead of having
I'm trying to test in app purchases in our app but I'm not having
I'm having a Sample.sqlite database in my app and can you help me on
In our app we store HTML in a db table and render that out
We're upgrading our app from Rails 2 to Rails 3.1, and I'm having trouble
Having a design discussion with some co-workers about our app. Looking for the best

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.