Skip to content

Instantly share code, notes, and snippets.

@DataZombies
Last active October 12, 2015 09:58
Show Gist options
  • Save DataZombies/4009752 to your computer and use it in GitHub Desktop.
Save DataZombies/4009752 to your computer and use it in GitHub Desktop.
Objective C Database Versioning from AppDelegate
//AppDelegate.h
@property(nonatomic,assign) NSArray *documentPaths;
@property(nonatomic,assign) NSError *error;
@property(nonatomic,assign) NSFileManager *fileManager;
@property(nonatomic,assign) NSString *documentsDir;
//AppDelegate.m
@synthesize error, fileManager, documentPaths, documentsDir;
...
- (id) init
{
...
// copy databases if they don't exist
fileManager = [NSFileManager defaultManager];
documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDir = [documentPaths objectAtIndex: 0];
[self copyFileFromBundleToDocs: @"Favorites": false]; // no version checking
[self copyFileFromBundleToDocs: @"Inventory": true]; // version checking enabled
// copy databases end
...
}
#pragma mark
#pragma mark database versioning
-(void)fileOperationErrorHandler: (NSString*) source {
NSLog(@"\n***** %@ error description - %@", source, [error localizedDescription]);
NSLog(@"***** %@ error reason - %@\n", source, [error localizedFailureReason]);
}
-(BOOL)copyFile: (NSString*)fileName: (NSString*)fileType {
error = nil;
if ([fileManager copyItemAtPath: [[NSBundle mainBundle] pathForResource: fileName ofType: fileType]
toPath: [documentsDir stringByAppendingPathComponent: fileName]
error: &error]) {
NSLog(@"Successfully copied %@ to %@.", fileName, documentsDir);
return true;
} else {
[self fileOperationErrorHandler: @"copyFile"];
return false;
}
}
-(BOOL)deleteFile: (NSString*)fileName {
error = nil;
if ([fileManager removeItemAtPath: [documentsDir stringByAppendingPathComponent: fileName]
error: &error]) {
NSLog (@"Deleted %@.", fileName);
return true;
} else {
[self fileOperationErrorHandler: @"deleteFile"];
return false;
}
}
-(BOOL)fileExists: (NSString*)fileName {
return [fileManager fileExistsAtPath: [documentsDir stringByAppendingPathComponent: fileName]];
}
-(NSString*)readDataFromFile: (NSString*)fileName {
NSString *filePath = [documentsDir stringByAppendingPathComponent: fileName];
NSString *data = nil;
error = nil;
if ([self fileExists: fileName]) {
data = [NSString stringWithContentsOfFile: filePath
encoding: NSUTF8StringEncoding
error: &error];
if (error == nil) {
NSLog (@"Read '%@' from %@.", data, fileName);
} else {
[self fileOperationErrorHandler: @"readVersionFromFile"];
data = nil;
}
}
return data;
}
-(bool)writeDataToFile: (NSString*) fileName: (NSString*) data {
error = nil;
if ([fileManager createFileAtPath: [documentsDir stringByAppendingPathComponent: fileName]
contents: [data dataUsingEncoding: NSUTF8StringEncoding]
attributes: nil]) {
NSLog (@"Wrote '%@' to %@.", data, fileName);
return true;
} else {
[self fileOperationErrorHandler: @"copyFile"];
return false;
}
}
-(void)copyFileFromBundleToDocs: (NSString*)fileName: (BOOL)versionCheck {
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSString *bundleVerson = [NSString stringWithFormat: @"%@ (%@)", [info objectForKey: @"CFBundleShortVersionString"], [info objectForKey: @"CFBundleVersion"]];
NSString *fileVersion = nil;
NSString *versionFileName = [NSString stringWithFormat: @"%@.version", fileName];
if ([self fileExists: fileName]) {
// db file exists
NSLog (@"%@ found.", fileName);
if (versionCheck) {
// compare version/build number with current values
NSLog (@"Checking version.");
fileVersion = [self readDataFromFile: versionFileName];
NSLog(@"(bundleVerson: %@ == fileVersion: %@) => %@", bundleVerson, fileVersion, [fileVersion isEqualToString: bundleVerson] ? @"true" : @"false");
// new db file in app bundle
if (! [fileVersion isEqualToString: bundleVerson]) {
// delete db file in documnets
if ([self deleteFile: fileName]) {
// copy app bundle db file to documents
if ([self copyFile: fileName: @""]) {
// write version/build number to the version file in the documents folder.
[self writeDataToFile: versionFileName: bundleVerson];
}
}
}
}
} else {
// db file doesn't exist
NSLog (@"%@ not found, copying.", fileName);
// copy db file to documents
if ([self copyFile: fileName: @""]) {
if (versionCheck) {
// write version/build number to the version file in the documents folder.
[self writeDataToFile: versionFileName: bundleVerson];
}
}
}
NSLog(@"Done with %@.", fileName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment