Skip to content

Commit 0d567d5

Browse files
committed
Proper failable methods for the LocalIdStore
1 parent 963b2d0 commit 0d567d5

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

Parse/Parse/Internal/Object/LocalIdStore/PFObjectLocalIdStore.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
///--------------------------------------
3939

4040
- (NSString *)createLocalId;
41-
- (void)retainLocalIdOnDisk:(NSString *)localId error:(NSError **)error;
42-
- (void)releaseLocalIdOnDisk:(NSString *)localId error:(NSError **)error;
41+
- (BOOL)retainLocalIdOnDisk:(NSString *)localId error:(NSError **)error;
42+
- (BOOL)releaseLocalIdOnDisk:(NSString *)localId error:(NSError **)error;
4343

44-
- (void)setObjectId:(NSString *)objectId forLocalId:(NSString *)localId error:(NSError **)error;
44+
- (BOOL)setObjectId:(NSString *)objectId forLocalId:(NSString *)localId error:(NSError **)error;
4545
- (NSString *)objectIdForLocalId:(NSString *)localId;
4646

4747
// For testing only.

Parse/Parse/Internal/Object/LocalIdStore/PFObjectLocalIdStore.m

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ - (PFObjectLocalIdStoreMapEntry *)getMapEntry:(NSString *)localId error:(NSError
154154
if (objectId) {
155155
entry.objectId = objectId;
156156
if (entry.referenceCount > 0) {
157-
[self putMapEntry:entry forLocalId:localId];
157+
if(![self putMapEntry:entry forLocalId:localId error:error]) {
158+
return nil;
159+
}
158160
}
159161
}
160162
}
@@ -165,21 +167,23 @@ - (PFObjectLocalIdStoreMapEntry *)getMapEntry:(NSString *)localId error:(NSError
165167
/**
166168
* Writes one entry to the local id map on disk.
167169
*/
168-
- (void)putMapEntry:(PFObjectLocalIdStoreMapEntry *)entry forLocalId:(NSString *)localId {
169-
PFConsistencyAssert([[self class] isLocalId:localId], @"Tried to get invalid local id: \"%@\".", localId);
170+
- (BOOL)putMapEntry:(PFObjectLocalIdStoreMapEntry *)entry forLocalId:(NSString *)localId error:(NSError **)error {
171+
PFConsistencyError(error, [[self class] isLocalId:localId], NO, @"Tried to get invalid local id: \"%@\".", localId);
170172

171173
NSString *file = [_diskPath stringByAppendingPathComponent:localId];
172174
[entry writeToFile:file];
175+
return YES;
173176
}
174177

175178
/**
176179
* Removes an entry from the local id map on disk.
177180
*/
178-
- (void)removeMapEntry:(NSString *)localId {
179-
PFConsistencyAssert([[self class] isLocalId:localId], @"Tried to get invalid local id: \"%@\".", localId);
181+
- (BOOL)removeMapEntry:(NSString *)localId error:(NSError **)error {
182+
PFConsistencyError(error, [[self class] isLocalId:localId], NO, @"Tried to get invalid local id: \"%@\".", localId);
180183

181184
NSString *file = [_diskPath stringByAppendingPathComponent:localId];
182185
[[NSFileManager defaultManager] removeItemAtPath:file error:nil];
186+
return YES;
183187
}
184188

185189
/**
@@ -203,49 +207,52 @@ - (NSString *)createLocalId {
203207
/**
204208
* Increments the retain count of a local id on disk.
205209
*/
206-
- (void)retainLocalIdOnDisk:(NSString *)localId error:(NSError **)error {
210+
- (BOOL)retainLocalIdOnDisk:(NSString *)localId error:(NSError **)error {
207211
@synchronized (_lock) {
208212
PFObjectLocalIdStoreMapEntry *entry = [self getMapEntry:localId error:error];
209-
if (!entry || error) {
210-
return;
213+
if (!entry && error) {
214+
return NO;
211215
}
212216
entry.referenceCount++;
213-
[self putMapEntry:entry forLocalId:localId];
217+
return [self putMapEntry:entry forLocalId:localId error:error];
214218
}
215219
}
216220

217221
/**
218222
* Decrements the retain count of a local id on disk.
219223
* If the retain count hits zero, the id is forgotten forever.
220224
*/
221-
- (void)releaseLocalIdOnDisk:(NSString *)localId error:(NSError **)error {
225+
- (BOOL)releaseLocalIdOnDisk:(NSString *)localId error:(NSError **)error {
222226
@synchronized (_lock) {
223227
PFObjectLocalIdStoreMapEntry *entry = [self getMapEntry:localId error:error];
224-
if (error || !entry) {
225-
return;
228+
if (!entry && error) {
229+
return NO;
226230
}
227231
if (--entry.referenceCount > 0) {
228-
[self putMapEntry:entry forLocalId:localId];
232+
return [self putMapEntry:entry forLocalId:localId error:error];
229233
} else {
230-
[self removeMapEntry:localId];
234+
return [self removeMapEntry:localId error:error];
231235
}
232236
}
233237
}
234238

235239
/**
236240
* Sets the objectId associated with a given local id.
237241
*/
238-
- (void)setObjectId:(NSString *)objectId forLocalId:(NSString *)localId error:(NSError **)error {
242+
- (BOOL)setObjectId:(NSString *)objectId forLocalId:(NSString *)localId error:(NSError **)error {
239243
@synchronized (_lock) {
240244
PFObjectLocalIdStoreMapEntry *entry = [self getMapEntry:localId error:error];
241-
if (error || !entry) {
242-
return;
245+
if (!entry && error) {
246+
return NO;
243247
}
244248
if (entry.referenceCount > 0) {
245249
entry.objectId = objectId;
246-
[self putMapEntry:entry forLocalId:localId];
250+
if (![self putMapEntry:entry forLocalId:localId error:error]) {
251+
return NO;
252+
}
247253
}
248254
_inMemoryCache[localId] = objectId;
255+
return YES;
249256
}
250257
}
251258

0 commit comments

Comments
 (0)