lundi 9 mai 2016

Removing and creating PersistentStore and sqlite database

I'm working on an app that exchanges data with a server, and the mobile device has an option to wipe the database clean. I've looked around here and I found out I'm using the same code that's been suggested.

BOOL removedPS = [self.persistentStoreCoordinator removePersistentStore:store error:&error];
BOOL removedSqlite = [[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];

I use the two BOOLs to check if the operations have been made correctly and everything seems to be working smooth. Even the double check

[[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]

says the file has been deleted. After that, I want to re-create Persistent Store and sqlite:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
   //ouch :/
}

but the addPersistentStoreWithType keeps on returning a "false" flag. Same goes with the sqlite: I try to copy a fresh .sqlite database, but I keep getting a "nil" for the NSString *param variable, and I can't understand why.

NSError* err = nil;
                NSBundle *bundle = [NSBundle mainBundle];
                NSString *path = @"BaseScope";
                NSString *type = @"sqlite";
                NSString *param = [bundle pathForResource:path ofType:type];

                //NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"BaseScope" ofType:@"sqlite"]];
                //NSURL *preloadURL = [NSURL fileURLWithPath:[bundle pathForResource:path ofType:type]];
                if (param != nil)
                {
                    NSURL *preloadURL = [NSURL fileURLWithPath:param isDirectory:false];

                    if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err])
                    {

                    }
                }

I always worked in the .NET environment and this is really puzzling me, but knocking the head against the monitor won't work. Any help appreciated. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire