Skip to content

Commit 9b44693

Browse files
committed
Merge pull request #373 from ParsePlatform/nlutsenko.config.stream
Improve PFConfig loading from disc performance by using JSON stream from file.
2 parents cfc3842 + fae3d80 commit 9b44693

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

Parse/Internal/Config/Controller/PFCurrentConfigController.m

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,8 @@ + (instancetype)controllerWithFileManager:(PFFileManager *)fileManager {
6161
- (BFTask *)getCurrentConfigAsync {
6262
return [BFTask taskFromExecutor:_dataExecutor withBlock:^id{
6363
if (!_currentConfig) {
64-
NSError *error = nil;
65-
NSData *jsonData = [NSData dataWithContentsOfFile:self.configFilePath
66-
options:NSDataReadingMappedIfSafe
67-
error:&error];
68-
if (error == nil && [jsonData length] != 0) {
69-
NSDictionary *dictionary = [PFJSONSerialization JSONObjectFromData:jsonData];
64+
NSDictionary *dictionary = [PFJSONSerialization JSONObjectFromFileAtPath:self.configFilePath];
65+
if (dictionary) {
7066
NSDictionary *decodedDictionary = [[PFDecoder objectDecoder] decodeObject:dictionary];
7167
_currentConfig = [[PFConfig alloc] initWithFetchedConfig:decodedDictionary];
7268
} else {

Parse/Internal/PFJSONSerialization.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#import <Foundation/Foundation.h>
1111

12+
NS_ASSUME_NONNULL_BEGIN
13+
1214
@interface PFJSONSerialization : NSObject
1315

1416
/*!
@@ -21,7 +23,7 @@
2123
2224
@returns NSData of JSON representing the passed in object.
2325
*/
24-
+ (NSData *)dataFromJSONObject:(id)object;
26+
+ (nullable NSData *)dataFromJSONObject:(id)object;
2527

2628
/*!
2729
The object passed in must be one of:
@@ -33,18 +35,31 @@
3335
3436
@returns NSString of JSON representing the passed in object.
3537
*/
36-
+ (NSString *)stringFromJSONObject:(id)object;
38+
+ (nullable NSString *)stringFromJSONObject:(id)object;
3739

3840
/*!
3941
Takes a JSON string and returns the NSDictionaries and NSArrays in it.
4042
You should still call decodeObject if you want Parse types.
4143
*/
42-
+ (id)JSONObjectFromData:(NSData *)data;
44+
+ (nullable id)JSONObjectFromData:(NSData *)data;
4345

4446
/*!
4547
Takes a JSON string and returns the NSDictionaries and NSArrays in it.
4648
You should still call decodeObject if you want Parse types.
4749
*/
48-
+ (id)JSONObjectFromString:(NSString *)string;
50+
+ (nullable id)JSONObjectFromString:(NSString *)string;
51+
52+
/*!
53+
@abstract Takes a file path to json file and returns the NSDictionaries and NSArrays in it.
54+
55+
@description You should still call decodeObject if you want Parse types.
56+
57+
@param filePath File path to a file.
58+
59+
@return Decoded object.
60+
*/
61+
+ (nullable id)JSONObjectFromFileAtPath:(NSString *)filePath;
4962

5063
@end
64+
65+
NS_ASSUME_NONNULL_END

Parse/Internal/PFJSONSerialization.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,23 @@ + (id)JSONObjectFromString:(NSString *)string {
4343
return [self JSONObjectFromData:[string dataUsingEncoding:NSUTF8StringEncoding]];
4444
}
4545

46+
+ (id)JSONObjectFromFileAtPath:(NSString *)filePath {
47+
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:filePath];
48+
if (!stream) {
49+
return nil;
50+
}
51+
52+
[stream open];
53+
54+
NSError *error = nil;
55+
id object = [NSJSONSerialization JSONObjectWithStream:stream options:0 error:&error];
56+
if (!object || error) {
57+
PFLogError(PFLoggingTagCommon, @"JSON deserialization failed with error: %@", error.description);
58+
}
59+
60+
[stream close];
61+
62+
return object;
63+
}
64+
4665
@end

0 commit comments

Comments
 (0)