fuzzygoat
fuzzygoat

Reputation: 26223

Core Data database closing between builds?

I am using Core Data for the first time and was just curious if what I am seeing is correct. Each time I run the application via Xcode it reports that the database, exists, is closed, and is being opened. The next time I run the app the same happens ...

My question is, I am not closing the database myself and I was just curious if I have something wrong somewhere or if iOS is closing the database itself.

EDIT_001: Code Added.

- (void)viewDidLoad {
    [super viewDidLoad];
    if([self planetDatabase] == nil) {

        // CREATE MANAGED DOCUMENT
        NSLog(@"Database: Setup");
        NSArray *userDocumentPath = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
        NSURL *databaseFileURL = [[userDocumentPath lastObject] URLByAppendingPathComponent:@"DefaultPlanetDatabase"];
        [self setPlanetDatabase:[[UIManagedDocument alloc] initWithFileURL:databaseFileURL]];

        // CHECK FOR EXISTING 
        if([[NSFileManager defaultManager] fileExistsAtPath:[databaseFileURL path]]) {

            // OPEN IF CLOSED
            if([[self planetDatabase] documentState] == UIDocumentStateClosed) {
                NSLog(@"Database: Closed");
                [[self planetDatabase] openWithCompletionHandler:^(BOOL success) {
                    if(success)[self doWhatsNext];
                }];
            // USE IF NORMAL
            } else if([[self planetDatabase] documentState] == UIDocumentStateNormal) {
                [self doWhatsNext];
            }
        // CREATE AND OPEN 
        } else {
            [[self planetDatabase] saveToURL: [[self planetDatabase] fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
                if(success)[self doWhatsNext];
            }];
        }
    }
}

The first time I run this code from within Xcode the database is created "CREATE AND OPEN" the next time I run this code "OPEN IF CLOSED" is called. I assume this is correct, but am just trying to verify that Xcode does indeed close the open database between builds.

Upvotes: 0

Views: 491

Answers (1)

Daniel Eggert
Daniel Eggert

Reputation: 6715

The SQLite database is not like, say MySQL. It's not a server running somewhere. It's just code inside your app. Hence, when you add a NSPersistenStore, the database is opened, and when your app shuts down, the database will be closed. That's how SQLite works.

Xcode doesn't do anything to your database. It's just your app touching it.

Upvotes: 1

Related Questions