Skip to content

Commit 637626a

Browse files
committed
Ability to revert all changes or changes for key
1 parent 670d7ec commit 637626a

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

Parse/PFObject.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ NS_REQUIRES_PROPERTY_DEFINITIONS
141141
/*!
142142
@abstract Sets the object associated with a given key.
143143
144-
@param object The object for `key`. A strong reference to the object is maintaned by PFObject.
144+
@param object The object for `key`. A strong reference to the object is maintained by PFObject.
145145
Raises an `NSInvalidArgumentException` if `object` is `nil`.
146146
If you need to represent a `nil` value - use `NSNull`.
147147
@param key The key for `object`.
@@ -176,7 +176,7 @@ NS_REQUIRES_PROPERTY_DEFINITIONS
176176
@discussion This method enables usage of literal syntax on `PFObject`.
177177
E.g. `object[@"key"] = @"value";`
178178
179-
@param object The object for `key`. A strong reference to the object is maintaned by PFObject.
179+
@param object The object for `key`. A strong reference to the object is maintained by PFObject.
180180
Raises an `NSInvalidArgumentException` if `object` is `nil`.
181181
If you need to represent a `nil` value - use `NSNull`.
182182
@param key The key for `object`.
@@ -202,6 +202,19 @@ NS_REQUIRES_PROPERTY_DEFINITIONS
202202
*/
203203
- (PFRelation *)relationforKey:(NSString *)key PARSE_DEPRECATED("Please use -relationForKey: instead.");
204204

205+
/*!
206+
@abstract Clears any changes to this object made since the last call to save and sets it back to the server state.
207+
*/
208+
- (void)revert;
209+
210+
/*!
211+
@abstract Clears any changes to this object's key that were done after last successful save and sets it back to the
212+
server state.
213+
214+
@param key The key to revert changes for.
215+
*/
216+
- (void)revertObjectForKey:(NSString *)key;
217+
205218
///--------------------------------------
206219
/// @name Array Accessors
207220
///--------------------------------------

Parse/PFObject.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,40 @@ - (void)removeObjectForKey:(NSString *)key {
23132313
}
23142314
}
23152315

2316+
- (void)revert {
2317+
@synchronized (lock) {
2318+
if ([self isDirty]) {
2319+
NSMutableArray *removedKeys = [NSMutableArray array];
2320+
[removedKeys addObjectsFromArray: [self allKeys]];
2321+
for (PFOperationSet *operationSet in operationSetQueue) {
2322+
for (NSString *key in operationSet.keyEnumerator) {
2323+
[removedKeys addObject:key];
2324+
}
2325+
}
2326+
2327+
PFOperationSet *changes = [self unsavedChanges];
2328+
for (NSString *key in changes.keyEnumerator) {
2329+
[changes removeObjectForKey:key];
2330+
}
2331+
2332+
[self rebuildEstimatedData];
2333+
[_availableKeys minusSet:[NSSet setWithArray:removedKeys]];
2334+
[self checkpointAllMutableContainers];
2335+
}
2336+
}
2337+
}
2338+
2339+
- (void)revertObjectForKey:(NSString *)key {
2340+
@synchronized (lock) {
2341+
if ([self isDirtyForKey:key]) {
2342+
[[self unsavedChanges] removeObjectForKey:key];
2343+
[self rebuildEstimatedData];
2344+
[_availableKeys removeObject:key];
2345+
[self checkpointAllMutableContainers];
2346+
}
2347+
}
2348+
}
2349+
23162350
#pragma mark Relations
23172351

23182352
- (PFRelation *)relationforKey:(NSString *)key {

Tests/Unit/ObjectUnitTests.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "PFObject.h"
1111
#import "PFUnitTestCase.h"
1212
#import "Parse_Private.h"
13+
#import "PFObjectPrivate.h"
1314

1415
@interface ObjectUnitTests : PFUnitTestCase
1516

@@ -182,4 +183,27 @@ - (void)testKeyValueCoding {
182183
XCTAssertEqualObjects([object valueForKey:@"yarr"], @"yolo");
183184
}
184185

186+
- (void)testRevert {
187+
NSDate *date = [NSDate date];
188+
NSNumber *number = @0.75;
189+
PFObject *object = [PFObject _objectFromDictionary:@{ @"yarr" : date, @"score": number } defaultClassName:@"Test" completeData:YES];
190+
object[@"yarr"] = @"yolo";
191+
[object revert];
192+
XCTAssertEqualObjects(object[@"yarr"], date);
193+
XCTAssertEqualObjects(object[@"score"], number);
194+
}
195+
196+
- (void)testRevertObjectForKey {
197+
NSDate *date = [NSDate date];
198+
NSNumber *number = @0.75;
199+
PFObject *object = [PFObject _objectFromDictionary:@{ @"yarr" : date,
200+
@"score" : @1.0 }
201+
defaultClassName:@"Test" completeData:YES];
202+
object[@"yarr"] = @"yolo";
203+
object[@"score"] = number;
204+
[object revertObjectForKey:@"yarr"];
205+
XCTAssertEqualObjects(object[@"yarr"], date);
206+
XCTAssertEqualObjects(object[@"score"], number);
207+
}
208+
185209
@end

0 commit comments

Comments
 (0)