Skip to content

Commit a359314

Browse files
committed
Fix decoding of updatedAt/createdAt on PFObject if dates are already decoded.
1 parent a4bc5d9 commit a359314

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Parse/PFObject.m

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,9 +1365,21 @@ - (void)_mergeFromServerWithResult:(NSDictionary *)result decoder:(PFDecoder *)d
13651365
if ([key isEqualToString:PFObjectObjectIdRESTKey]) {
13661366
state.objectId = obj;
13671367
} else if ([key isEqualToString:PFObjectCreatedAtRESTKey]) {
1368-
[state setCreatedAtFromString:obj];
1368+
// These dates can be passed in as NSDate or as NSString,
1369+
// depending on whether they were wrapped inside JSONObject with __type: Date or not.
1370+
if ([obj isKindOfClass:[NSDate class]]) {
1371+
state.createdAt = obj;
1372+
} else {
1373+
[state setCreatedAtFromString:obj];
1374+
}
13691375
} else if ([key isEqualToString:PFObjectUpdatedAtRESTKey]) {
1370-
[state setUpdatedAtFromString:obj];
1376+
// These dates can be passed in as NSDate or as NSString,
1377+
// depending on whether they were wrapped inside JSONObject with __type: Date or not.
1378+
if ([obj isKindOfClass:[NSDate class]]) {
1379+
state.updatedAt = obj;
1380+
} else {
1381+
[state setUpdatedAtFromString:obj];
1382+
}
13711383
} else if ([key isEqualToString:PFObjectACLRESTKey]) {
13721384
PFACL *acl = [PFACL ACLWithDictionary:obj];
13731385
[state setServerDataObject:acl forKey:key];

Tests/Unit/DecoderTests.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,31 @@ - (void)testDecodingObjects {
165165
XCTAssertEqualObjects(object.objectId, @"123");
166166
}
167167

168+
- (void)testDecodingObjectsWithDates {
169+
PFDecoder *decoder = [[PFDecoder alloc] init];
170+
171+
NSDictionary *decoded = [decoder decodeObject:@{ @"object" : @{@"__type" : @"Object",
172+
@"className" : @"Yolo",
173+
@"objectId" : @"123",
174+
@"updatedAt" : @"1970-01-01T00:00:01.000Z",
175+
@"createdAt" : @"1970-01-01T00:00:02.000Z"} }];
176+
PFObject *object = decoded[@"object"];
177+
178+
XCTAssertEqualObjects(object.updatedAt, [NSDate dateWithTimeIntervalSince1970:1.0]);
179+
XCTAssertEqualObjects(object.createdAt, [NSDate dateWithTimeIntervalSince1970:2.0]);
180+
181+
decoded = [decoder decodeObject:@{ @"object" : @{@"__type" : @"Object",
182+
@"className" : @"Yolo",
183+
@"objectId" : @"123",
184+
@"updatedAt" : @{@"__type" : @"Date",
185+
@"iso" : @"1970-01-01T00:00:01.000Z"},
186+
@"createdAt" : @{@"__type" : @"Date",
187+
@"iso" : @"1970-01-01T00:00:02.000Z"}} }];
188+
object = decoded[@"object"];
189+
XCTAssertEqualObjects(object.updatedAt, [NSDate dateWithTimeIntervalSince1970:1.0]);
190+
XCTAssertEqualObjects(object.createdAt, [NSDate dateWithTimeIntervalSince1970:2.0]);
191+
}
192+
168193
- (void)testDecodingUnknownType {
169194
PFDecoder *decoder = [[PFDecoder alloc] init];
170195

0 commit comments

Comments
 (0)