Skip to content

Commit 899a5da

Browse files
committed
Merge pull request #70 from kashif/issue-52
Ability to revert all changes or changes for key
2 parents 9abf6e7 + f6b51b2 commit 899a5da

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,38 @@ - (void)removeObjectForKey:(NSString *)key {
23132313
}
23142314
}
23152315

2316+
- (void)revert {
2317+
@synchronized (self.lock) {
2318+
if ([self isDirty]) {
2319+
NSMutableSet *persistentKeys = [NSMutableSet setWithArray:[self._state.serverData allKeys]];
2320+
2321+
PFOperationSet *unsavedChanges = [self unsavedChanges];
2322+
for (PFOperationSet *operationSet in operationSetQueue) {
2323+
if (operationSet != unsavedChanges) {
2324+
[persistentKeys addObjectsFromArray:[operationSet.keyEnumerator allObjects]];
2325+
}
2326+
}
2327+
2328+
[unsavedChanges removeAllObjects];
2329+
[_availableKeys intersectSet:persistentKeys];
2330+
2331+
[self rebuildEstimatedData];
2332+
[self checkpointAllMutableContainers];
2333+
}
2334+
}
2335+
}
2336+
2337+
- (void)revertObjectForKey:(NSString *)key {
2338+
@synchronized (self.lock) {
2339+
if ([self isDirtyForKey:key]) {
2340+
[[self unsavedChanges] removeObjectForKey:key];
2341+
[self rebuildEstimatedData];
2342+
[_availableKeys removeObject:key];
2343+
[self checkpointAllMutableContainers];
2344+
}
2345+
}
2346+
}
2347+
23162348
#pragma mark Relations
23172349

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

Tests/Unit/ObjectUnitTests.m

Lines changed: 29 additions & 1 deletion
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

@@ -186,7 +187,7 @@ - (void)testKeyValueCoding {
186187

187188
- (void)testFetchObjectWithoutObjectIdError {
188189
PFObject *object = [PFObject objectWithClassName:@"Test"];
189-
190+
190191
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
191192
[[object fetchInBackground] continueWithBlock:^id(BFTask *task) {
192193
XCTAssertNotNil(task.error);
@@ -198,4 +199,31 @@ - (void)testFetchObjectWithoutObjectIdError {
198199
[self waitForTestExpectations];
199200
}
200201

202+
#pragma mark Revert
203+
204+
- (void)testRevert {
205+
NSDate *date = [NSDate date];
206+
NSNumber *number = @0.75;
207+
PFObject *object = [PFObject _objectFromDictionary:@{ @"yarr" : date,
208+
@"score": number }
209+
defaultClassName:@"Test" completeData:YES];
210+
object[@"yarr"] = @"yolo";
211+
[object revert];
212+
XCTAssertEqualObjects(object[@"yarr"], date);
213+
XCTAssertEqualObjects(object[@"score"], number);
214+
}
215+
216+
- (void)testRevertObjectForKey {
217+
NSDate *date = [NSDate date];
218+
NSNumber *number = @0.75;
219+
PFObject *object = [PFObject _objectFromDictionary:@{ @"yarr" : date,
220+
@"score" : @1.0 }
221+
defaultClassName:@"Test" completeData:YES];
222+
object[@"yarr"] = @"yolo";
223+
object[@"score"] = number;
224+
[object revertObjectForKey:@"yarr"];
225+
XCTAssertEqualObjects(object[@"yarr"], date);
226+
XCTAssertEqualObjects(object[@"score"], number);
227+
}
228+
201229
@end

0 commit comments

Comments
 (0)