-
Notifications
You must be signed in to change notification settings - Fork 0
/
PListDb.m
143 lines (97 loc) · 2.76 KB
/
PListDb.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// PListDb.m
//
// Licensed by ruralcoder.com under the
// Creative Commons Attribution-ShareAlike 3.0 Unported License
#import "PListDb.h"
#import "Application.h"
#import "NSDictionaryExtension.h"
#import "ApplicationLogging.h"
NSString *const NKPListDbDataUpdated = @"NKPListDbDataUpdated";
NSString *const NKPListDbDataUpdateFailed = @"NKPListDbDataUpdateFailed";
@implementation PListDb
@synthesize dbName = _dbName;
@synthesize absolutePath = _absolutePath;
@synthesize fields = _fields;
@synthesize notificationKey;
+ (id) instance
{
NSAssert(NO, @"You are trying to instantiate the base class");
return nil;
}
- (BOOL) save
{
return [self saveAndNotify:YES];
}
- (BOOL) saveAndNotify:(BOOL)notifyObservers
{
BOOL saved = [self.fields writeToFile:_absolutePath atomically:YES];
if (notifyObservers)
{
id notifKey = (self.notificationKey) ? self.notificationKey : NKPListDbDataUpdated;
if (saved)
[[NSNotificationCenter defaultCenter] postNotificationName:notifKey object:nil];
else
[[NSNotificationCenter defaultCenter] postNotificationName:NKPListDbDataUpdateFailed object:nil];
}
#if TARGET_IPHONE_SIMULATOR
if (saved)
DLog(@"%@::saveAndNotify: OK '%@'.", [self class], _dbName);
else
ErrorLog(@"Error: Unable to save '%@'.", _dbName);
#endif
return saved;
}
- (void) reloadData
{
[self.fields removeAllObjects];
[_fields release];
_fields = [[NSMutableDictionary alloc] initWithContentsOfFile:self.absolutePath];
}
- (void) addObject:(id)object forKey:(id)key
{
[self addObject:object forKey:key overwrite:NO];
}
- (void) addObject:(id)object forKey:(id)key overwrite:(BOOL)overwrite
{
if (overwrite == NO && [self.fields containsKey:key] == YES)
return;
[self.fields setObject:object forKey:key];
[self save];
}
- (void) removeObjectWithKey:(id)key
{
if ([self.fields containsKey:key] == NO)
return;
[self.fields removeObjectForKey:key];
[self save];
}
- (void) removeAllObjects
{
[self.fields removeAllObjects];
[self save];
}
#pragma mark Alloc & Dealloc
- (id) initWithDbName:(id)dbName
{
id absolutePath = [Application fileInCache:dbName];
self = [super init];
if (self)
{
_fields = [[NSMutableDictionary alloc] initWithContentsOfFile:absolutePath];
if (_fields == nil)
_fields = [[NSMutableDictionary alloc] init];
_dbName = [dbName retain];
_absolutePath = [absolutePath retain];
}
return self;
}
- (void) dealloc
{
[_fields release];
[_dbName release];
[_absolutePath release];
self.notificationKey = nil;
[super dealloc];
}
@end