Fix crash when popover is dismissed while a view is presented modally.
[pithos-ios] / Classes / Archiver.m
1 //
2 //  Archiver.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 10/4/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "Archiver.h"
10
11 @implementation Archiver
12
13 + (id)retrieve:(NSString *)key {
14     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
15     NSString *documentsDirectory = [paths objectAtIndex:0];
16     NSString *filePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.archive", key]];
17 //    NSLog(@"filepath: %@", filePath);
18     return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
19     /* TODO: fixme on potential leak - "retrieve" doesn't match copy or alloc pattern, and hence should be returning autoreleased object
20      * Not changing right now, but documenting for later fixing - need to see how and where [Archiver.. retrieve:key] is used to make sure 
21      * switching this won't cause a crash
22      */
23 }
24
25 + (void)persist:(id)object key:(NSString *)key {
26 //    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
27 //    dispatch_async(queue, ^{        
28         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
29         NSString *documentsDirectory = [paths objectAtIndex:0];
30         NSString *filePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.archive", key]];
31         [NSKeyedArchiver archiveRootObject:object toFile:filePath];
32 //    });
33 }
34
35 + (BOOL)delete:(NSString *)key {
36     NSFileManager *fileManager = [NSFileManager defaultManager];
37     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
38     NSString *documentsDirectory = [paths objectAtIndex:0];
39     NSString *filePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.archive", key]];
40     return [fileManager removeItemAtPath:filePath error:NULL];    
41 }
42
43 + (BOOL)deleteEverything {
44     BOOL result = YES;
45     NSFileManager *fileManager = [NSFileManager defaultManager];
46     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
47     NSString *documentsDirectory = [paths objectAtIndex:0];
48     
49     NSArray *files = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];
50     
51     for (int i = 0; i < [files count]; i++) {
52         NSString *path = [files objectAtIndex:i];
53         result = result && [fileManager removeItemAtPath:path error:NULL];
54     }
55     
56     return result;
57 }
58
59 @end