Hello All,
I am wanting to append the Date and Time to the sqlite file name I am uploading to dropbox. what is the correct way to do this? below is the code I have thus far for uploading to the dropbox service, it works perfectly. I am also wanting to be able to strip the date and time stamp from the newest file (The date which is closest to the now() date, so the most recent backup can be retrieved. any help is appreciated.
Thanks,
James
Here's the code:
#pragma mark -
#pragma mark Core Data stack
//////////////////////////////////////////////////////////////////
// Returns the managed object context for the application. //
// If the context doesn't already exist, it is created and //
// bound to the persistent store coordinator for the //
// application. //
//////////////////////////////////////////////////////////////////
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [NSManagedObjectContext new];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
//////////////////////////////////////////////////////////////////
// Returns the managed object model for the application. //
// If the model doesn't already exist, it is created by //
// merging all of the models found in the application bundle. //
//////////////////////////////////////////////////////////////////
- (NSManagedObjectModel *) managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles: nil] retain];
return managedObjectModel;
}
//////////////////////////////////////////////////////////////////
// Returns the persistent store coordinator for the application.//
// If the coordinator doesn't already exist, it is created and //
// the application's store added to it. //
//////////////////////////////////////////////////////////////////
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: kPersistentStore];
//////////////////////////////////////////////////////////////////
// Set up the store. //
// For the sake of illustration, provide a pre-populated //
// default store. //
//////////////////////////////////////////////////////////////////
NSFileManager *fileManager = [NSFileManager defaultManager];
//////////////////////////////////////////////////////////////////
// If the expected store doesn't exist, copy the default store. //
//////////////////////////////////////////////////////////////////
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource: @"/database" ofType: @"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath: defaultStorePath toPath: storePath error: NULL];
}
}
NSURL *storeUrl = [NSURL fileURLWithPath: storePath];
NSError *error;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType
configuration: nil
URL: storeUrl
options: nil
error: &error]) {
//////////////////////////////////////////////////////////////////
// //
// Replace this implementation with code to handle the error //
// appropriately. abort() causes the application to generate a //
// crash log and terminate. You should not use this function in //
// a shipping application, although it may be useful during //
// development. If it is not possible to recover from the error,//
// display an alert panel that instructs the user to quit the //
// application by pressing the Home button. //
// //
// Typical reasons for an error here include: //
// ***> The persistent store is not accessible //
// ***> The schema for the persistent store is incompatible with//
// current managed object model. //
// //
// Check the error message to determine what the //
// actual problem was. //
// //
//////////////////////////////////////////////////////////////////
NSLog (@"Unresolved error %@, %@", error, [error userInfo]);
abort ();
}
return persistentStoreCoordinator;
}
#pragma mark -
#pragma mark Download the File from DropBox Cloud
//////////////////////////////////////////////////////////////////
// DOWNLOAD FROM DROPBOX: //
//////////////////////////////////////////////////////////////////
- (IBAction) downloadFromDropBox {
documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [documentPaths objectAtIndex:0];
[self.restClient loadFile: [NSString stringWithFormat: @"%@/%@", dropBoxFolder, dbFileName]
intoPath: [NSTemporaryDirectory () stringByAppendingPathComponent: dbFileName]];
UIAlertView *error1 = [[UIAlertView alloc] initWithTitle: @"Your database has been downloaded from DropBox"
message: @""
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[error1 show];
[error1 release];
}
- (void) restClient: (DBRestClient*) client loadedFile: (NSString*) destPath contentType: (NSString*) contentType {
[(DoctorsAppDelegate *) [UIApplication sharedApplication].delegate openPersitentStoreAtPath: destPath];
}
- (void) restClient: (DBRestClient*) client loadFileFailedWithError: (NSError*)error {
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle: @"Download Failed"
message: @""
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[errorView show];
[errorView release];
}
#pragma mark -
#pragma mark Upload the File to DropBox
//////////////////////////////////////////////////////////////////
// UPLOAD TO DROPBOX: //
//////////////////////////////////////////////////////////////////
- (IBAction) uploadToDropBox {
[self.restClient uploadFile: dbFileName
toPath: dropBoxFolder
fromPath: [[self applicationDocumentsDirectory]stringByAppendingPathComponent: dbFileName]];
[self.restClient uploadFile: dbFileName
toPath: dropBoxFolderHidden
fromPath: [[self applicationDocumentsDirectory]stringByAppendingPathComponent: dbFileName]];
UIAlertView *error2 = [[UIAlertView alloc] initWithTitle: @"Your database has been uploaded to DropBox"
message: @""
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[error2 show];
[error2 release];
}
#pragma mark -
#pragma mark Application's documents directory
//////////////////////////////////////////////////////////////////
// RETURNS THE PATH TO THE APPLICATIONS DOCUMENTS DIRECTORY: //
//////////////////////////////////////////////////////////////////
- (NSString *) applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
- (void) dealloc {
[managedObjectContext release];
[managedObjectModel release];
[persistentStoreCoordinator release];
[link release];
[download release];
[upload release];
[super dealloc];
}
@end
Replies
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAny suggestions on how toselect the file on the server with the date closest to the Now() date? and remove the appended date string??
Thanks You for your assistance,
James
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSo put where you want before send it, just remember the new path to upload it to dropbox. I not read your code, because you haven't use tag
an information like that should be not on file name, I would use last edit date directly from dropbox, as you can see in documentation you can retrieve it.
However you can do also as you said, once that you have an array of file, just take the file that start with Database- and then just scrape the data, and convert to NSData so that you will have an array of NSData, compare to retrieve the most recent and then download the file renaming it.
I will not write the code for you.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome