-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBManager.m
More file actions
178 lines (124 loc) · 4.74 KB
/
DBManager.m
File metadata and controls
178 lines (124 loc) · 4.74 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// DBManager.m
// MackNotas
//
// Created by Caio Remedio on 23/07/15.
// Copyright (c) 2015 Caio Remedio. All rights reserved.
//
#import "DBManager.h"
#import "MNNota+Persistence.h"
#import "MNFalta+Persistence.h"
#import "MNHorario+Persistence.h"
#import "MNCalendario+Persistence.h"
#import "MNAc+Persistence.h"
NSString * const DBManagerWillUpdateNotification = @"DBManagerWillUpdateNotification";
NSString * const DBManagerDidUpdateNotification = @"DBManagerDidUpdateNotification";
@implementation DBManager
static DBManager *SINGLETON = nil;
static bool isFirstAccess = YES;
#pragma mark - Public Method
+ (id)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
isFirstAccess = NO;
SINGLETON = [[super allocWithZone:NULL] init];
});
return SINGLETON;
}
- (id) init {
if(SINGLETON){
return SINGLETON;
}
if (isFirstAccess) {
[self doesNotRecognizeSelector:_cmd];
}
self = [super init];
if (self) {
_database = [[YapDatabase alloc] initWithPath:[GeneralHelper pathForDBFile]];
[MNNota registerViewsInDatabase:self.database];
[MNFalta registerViewsInDatabase:self.database];
[MNHorario registerViewsInDatabase:self.database];
[MNCalendario registerViewsInDatabase:self.database];
[MNAc registerViewsInDatabase:self.database];
_uiConnection = [_database newConnection];
_uiConnection.name = @"uiConnection";
_rwConnection = [_database newConnection];
_rwConnection.name = @"rwConnection";
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(yapDatabaseModified:)
name:YapDatabaseModifiedNotification
object:_database];
}
return self;
}
#pragma mark - Public Methods
+ (void)clearAllData {
[[DBManager sharedInstance].rwConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction * transaction) {
[transaction removeAllObjectsInAllCollections];
}];
}
+ (BOOL)shouldUpdateModel:(NSString *)modelClass
withMappings:(YapDatabaseViewMappings *)mappings {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *lastUpdate;
BOOL shouldUpdate = NO;
if ([modelClass isEqualToString:NSStringFromClass([MNNota class])]) {
lastUpdate = [defaults objectForKey:LAST_UPDATE_NOTAS];
shouldUpdate = lastUpdate ? fabs([lastUpdate timeIntervalSinceNow]) > CACHE_TIME_NOTAS : YES;
shouldUpdate = [mappings numberOfItemsInAllGroups] == 0 ?: shouldUpdate;
}
else if ([modelClass isEqualToString:NSStringFromClass([MNHorario class])]) {
lastUpdate = [defaults objectForKey:LAST_UPDATE_HORARIO];
shouldUpdate = lastUpdate ? fabs([lastUpdate timeIntervalSinceNow]) > CACHE_TIME_HORARIO : YES;
shouldUpdate = [mappings numberOfItemsInAllGroups] == 0 ?: shouldUpdate;
}
else if ([modelClass isEqualToString:NSStringFromClass([MNCalendario class])]) {
lastUpdate = [defaults objectForKey:LAST_UPDATE_CALENDARIO];
shouldUpdate = lastUpdate ? fabs([lastUpdate timeIntervalSinceNow]) > CACHE_TIME_CALENDARIO : YES;
shouldUpdate = [mappings numberOfItemsInAllGroups] == 0 ?: shouldUpdate;
}
else if ([modelClass isEqualToString:NSStringFromClass([MNAc class])]) {
lastUpdate = [defaults objectForKey:LAST_UPDATE_AC];
shouldUpdate = lastUpdate ? fabs([lastUpdate timeIntervalSinceNow]) > CACHE_TIME_AC : YES;
shouldUpdate = [mappings numberOfItemsInAllGroups] == 0 ?: shouldUpdate;
}
return shouldUpdate;
}
#pragma mark - Notification Center
- (void)yapDatabaseModified:(NSNotification *)notification {
// Notify observers we're about to update the database connection
[[NSNotificationCenter defaultCenter] postNotificationName:DBManagerWillUpdateNotification object:self];
// Move uiDatabaseConnection to the latest commit.
// Do so atomically, and fetch all the notifications for each commit we jump.
NSArray *notifications = [self.uiConnection beginLongLivedReadTransaction];
// Notify observers that the uiDatabaseConnection was updated
NSDictionary *userInfo = @{ @"notifications": notifications };
[[NSNotificationCenter defaultCenter]
postNotificationName:DBManagerDidUpdateNotification
object:self
userInfo:userInfo];
}
#pragma mark - Life Cycle
+ (id) allocWithZone:(NSZone *)zone
{
return [self sharedInstance];
}
+ (id)copyWithZone:(struct _NSZone *)zone
{
return [self sharedInstance];
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone
{
return [self sharedInstance];
}
- (id)copy
{
return [[DBManager alloc] init];
}
- (id)mutableCopy
{
return [[DBManager alloc] init];
}
@end