Skip to content

Commit 5485a3e

Browse files
author
Phil Jacobsen
committed
Fix for case where cached date is stored as an NSDictionary containing the ISO date.
1 parent cc33be6 commit 5485a3e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Parse/PFObject.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,13 +1282,34 @@ - (void)_mergeAfterSaveWithResult:(NSDictionary *)result decoder:(PFDecoder *)de
12821282
}
12831283
}
12841284

1285+
static NSString* ISO8601Format = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
1286+
// http://stackoverflow.com/questions/17558859/convert-iso-8601-to-nsdate
1287+
static NSDate* NSDateFromISO8601String(NSString* dateString) {
1288+
1289+
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
1290+
[formatter setDateFormat:ISO8601Format];
1291+
// Always use this locale when parsing fixed format date strings
1292+
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
1293+
[formatter setLocale:posix];
1294+
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
1295+
NSDate *date = [formatter dateFromString:dateString];
1296+
1297+
return date;
1298+
}
1299+
12851300
- (void)_mergeFromServerWithResult:(NSDictionary *)result decoder:(PFDecoder *)decoder completeData:(BOOL)completeData {
12861301
@synchronized (lock) {
12871302
self._state = [self._state copyByMutatingWithBlock:^(PFMutableObjectState *state) {
12881303
// If the server's data is complete, consider this object to be fetched.
12891304
state.complete |= completeData;
12901305

12911306
[result enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
1307+
if ([obj isKindOfClass:[NSDictionary class]]) {
1308+
if (([[obj valueForKey:@"__type"] isEqualToString:@"Date"]) &&
1309+
([obj valueForKey:@"iso"])) {
1310+
obj = NSDateFromISO8601String([obj valueForKey:@"iso"]);
1311+
}
1312+
}
12921313
if ([key isEqualToString:PFObjectObjectIdRESTKey]) {
12931314
state.objectId = obj;
12941315
} else if ([key isEqualToString:PFObjectCreatedAtRESTKey]) {

0 commit comments

Comments
 (0)