From 41c856b3c96e6961faaba3d9e937d93bb8543847 Mon Sep 17 00:00:00 2001 From: Giuseppe Nucifora Date: Wed, 3 Feb 2016 02:01:13 +0100 Subject: [PATCH 1/2] - Fix vari --- .../xcschemes/PNObject-Example.xcscheme | 37 + Example/PNObject/PNObjectAppDelegate.m | 67 +- Example/PNObject/main.m | 6 +- Example/Podfile.lock | 4 +- .../Pods/Local Podspecs/PNObject.podspec.json | 6 +- Example/Pods/Manifest.lock | 4 +- Example/Pods/Pods.xcodeproj/project.pbxproj | 1052 ++++++++--------- .../xcshareddata/xcschemes/PNObject.xcscheme | 2 +- .../Target Support Files/PNObject/Info.plist | 2 +- .../PNObject/PNObject-umbrella.h | 9 +- .../ResourceBundle-PNObject-Info.plist | 2 +- PNObject.podspec | 2 +- Pod/Classes/PNClasses/PNAccessToken.h | 25 +- Pod/Classes/PNClasses/PNAccessToken.m | 32 +- Pod/Classes/PNClasses/PNUser.h | 4 +- Pod/Classes/PNObject+Protected.m | 10 +- Pod/Classes/PNObject.m | 29 +- Pod/Classes/PNObjectConfig.m | 182 +-- 18 files changed, 794 insertions(+), 681 deletions(-) diff --git a/Example/PNObject.xcodeproj/xcshareddata/xcschemes/PNObject-Example.xcscheme b/Example/PNObject.xcodeproj/xcshareddata/xcschemes/PNObject-Example.xcscheme index 9861ec6..7af5b08 100644 --- a/Example/PNObject.xcodeproj/xcshareddata/xcschemes/PNObject-Example.xcscheme +++ b/Example/PNObject.xcodeproj/xcshareddata/xcschemes/PNObject-Example.xcscheme @@ -71,7 +71,44 @@ ReferencedContainer = "container:PNObject.xcodeproj"> + + + + + + + + + + + + + + + + diff --git a/Example/Pods/Target Support Files/PNObject/Info.plist b/Example/Pods/Target Support Files/PNObject/Info.plist index 161a9d3..421cf1d 100644 --- a/Example/Pods/Target Support Files/PNObject/Info.plist +++ b/Example/Pods/Target Support Files/PNObject/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.1.0 + 0.2.3 CFBundleSignature ???? CFBundleVersion diff --git a/Example/Pods/Target Support Files/PNObject/PNObject-umbrella.h b/Example/Pods/Target Support Files/PNObject/PNObject-umbrella.h index a44a0b5..716dd18 100644 --- a/Example/Pods/Target Support Files/PNObject/PNObject-umbrella.h +++ b/Example/Pods/Target Support Files/PNObject/PNObject-umbrella.h @@ -1,15 +1,16 @@ #import +#import "PNAccessToken.h" +#import "PNAddress.h" +#import "PNInstallation.h" +#import "PNLocation.h" +#import "PNUser.h" #import "PNObject+PNObjectConnection.h" #import "PNObject+Protected.h" #import "PNObject.h" #import "PNObjectConfig.h" #import "PNObjectConstants.h" #import "PNObjectModel.h" -#import "PNObjectProperty.h" -#import "PNAddress.h" -#import "PNLocation.h" -#import "PNUser.h" FOUNDATION_EXPORT double PNObjectVersionNumber; FOUNDATION_EXPORT const unsigned char PNObjectVersionString[]; diff --git a/Example/Pods/Target Support Files/PNObject/ResourceBundle-PNObject-Info.plist b/Example/Pods/Target Support Files/PNObject/ResourceBundle-PNObject-Info.plist index aec6281..8c3cae2 100644 --- a/Example/Pods/Target Support Files/PNObject/ResourceBundle-PNObject-Info.plist +++ b/Example/Pods/Target Support Files/PNObject/ResourceBundle-PNObject-Info.plist @@ -13,7 +13,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 0.1.0 + 0.2.3 CFBundleSignature ???? CFBundleVersion diff --git a/PNObject.podspec b/PNObject.podspec index 8ed2022..58512e2 100644 --- a/PNObject.podspec +++ b/PNObject.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = "PNObject" -s.version = "0.2.3" +s.version = "0.2.4" s.summary = "PNObject is a simple replica of the more complex ParseObject" # This description is used to generate tags and improve search results. diff --git a/Pod/Classes/PNClasses/PNAccessToken.h b/Pod/Classes/PNClasses/PNAccessToken.h index 0ac01c8..4f5d51b 100644 --- a/Pod/Classes/PNClasses/PNAccessToken.h +++ b/Pod/Classes/PNClasses/PNAccessToken.h @@ -6,7 +6,7 @@ // // -#import +#import "PNObject.h" @interface PNAccessToken : PNObject @@ -14,6 +14,20 @@ typedef NS_ENUM(NSInteger, TokenType) { TokenTypeBasic = 1, TokenTypeBearer = 2 }; + +/** + * gets singleton object of current user session. + * + * @return singleton + */ ++ (instancetype _Nonnull) currentUser; + + +///-------------------------------------- +#pragma mark - PNAccessToken Properties +///-------------------------------------- + + /** * <#Description#> */ @@ -22,9 +36,18 @@ typedef NS_ENUM(NSInteger, TokenType) { * <#Description#> */ @property (nonatomic, strong, nullable) NSDate *expirationDate; + +@property (nonatomic, strong, nullable) NSNumber *expiresIn; /** * <#Description#> */ @property (nonatomic) TokenType tokenType; +@property (nonatomic, strong, nullable) NSString *tokenTypeString; + +@property (nonatomic, strong, nullable) NSString *scope; + +@property (nonatomic, strong, nullable) NSString *refreshToken; + + @end diff --git a/Pod/Classes/PNClasses/PNAccessToken.m b/Pod/Classes/PNClasses/PNAccessToken.m index b3ba879..68a097e 100644 --- a/Pod/Classes/PNClasses/PNAccessToken.m +++ b/Pod/Classes/PNClasses/PNAccessToken.m @@ -7,6 +7,7 @@ // #import "PNAccessToken.h" +#import @interface PNAccessToken() @@ -19,13 +20,35 @@ + (NSDictionary *)objcetMapping { NSDictionary *mapping = @{ - @"accessToken":@"accessToken", - @"expirationDate":@"expirationDate", - @"tokenType":@"tokenType", + @"accessToken":@"access_token", + @"expiresIn":@"expires_in", + @"tokenTypeString":@"token_type", + @"tokenType":@"scope", + @"refreshToken":@"refresh_token", }; return mapping; } +- (instancetype) initWithJSON:(NSDictionary *)JSON { + self = [super initWithJSON:JSON]; + + if (self) { + ((void (^)())@{ + @"beaer" : ^{ + _tokenType = TokenTypeBearer; + }, + @"basic" : ^{ + _tokenType = TokenTypeBasic; + } + }[_tokenTypeString] ?: ^{ + + })(); + + _expirationDate = [[NSDate date] dateByAddingHours:[_expiresIn integerValue]]; + } + return self; +} + + (NSString *)objectClassName { return @"AccessToken"; } @@ -38,7 +61,4 @@ - - - @end diff --git a/Pod/Classes/PNClasses/PNUser.h b/Pod/Classes/PNClasses/PNUser.h index 62f5ecb..897bcfa 100644 --- a/Pod/Classes/PNClasses/PNUser.h +++ b/Pod/Classes/PNClasses/PNUser.h @@ -7,7 +7,7 @@ // #import "PNObject.h" - +#import "PNAccessToken.h" @interface PNUser : PNObject @@ -33,7 +33,7 @@ //- (void) setPassword:(NSString * _Nonnull)password inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id responseObject, NSError * error)) responseBlock; ///-------------------------------------- -#pragma mark - PNLocation Properties +#pragma mark - PNUser Properties ///-------------------------------------- /** diff --git a/Pod/Classes/PNObject+Protected.m b/Pod/Classes/PNObject+Protected.m index 16f712f..4bc7c8d 100644 --- a/Pod/Classes/PNObject+Protected.m +++ b/Pod/Classes/PNObject+Protected.m @@ -27,6 +27,9 @@ NSDictionary *properties = [PNObject propertiesForClass:self.class]; for (NSString *propertyName in properties) { + if ([propertyName isEqualToString:@"description"] || [propertyName isEqualToString:@"debugDescription"]) { + continue; + } [self addObserver:self forKeyPath:propertyName options:NSKeyValueObservingOptionNew context:nil]; } @@ -39,8 +42,11 @@ NSString *mappedJSONType; NSString *propertyType = [properties valueForKey:propertyName]; + NSLogDebug(@"%@",self.objectMapping); - id mappingValue = [self.objectMapping valueForKey:propertyName]; + NSLogDebug(@"%@",[[self class] objcetMapping]); + + id mappingValue = [[[self class] objcetMapping] valueForKey:propertyName]; if([mappingValue isKindOfClass:NSDictionary.class]) { mappedJSONKey = [mappingValue valueForKey:@"key"]; @@ -55,7 +61,7 @@ } // Get JSON value for the mapped key - id value = [JSON valueForKeyPath:propertyName]; + id value = [JSON valueForKeyPath:mappedJSONKey]; ((void (^)())@{ diff --git a/Pod/Classes/PNObject.m b/Pod/Classes/PNObject.m index c17adb3..f6392dd 100644 --- a/Pod/Classes/PNObject.m +++ b/Pod/Classes/PNObject.m @@ -259,6 +259,7 @@ return _JSON; } + - (NSDictionary* _Nonnull) getJSONObject { if (!_JSON) { return [self reverseMapping]; @@ -307,7 +308,9 @@ - (id _Nonnull) saveLocally { - return [_objectModel saveLocally:self]; + __weak id weakSelf = self; + + return [_objectModel saveLocally:weakSelf]; } - (BOOL) autoRemoveLocally { @@ -318,20 +321,26 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - [_JSON setObject:[change objectForKey:@"new"] forKey:keyPath]; + NSLogDebug(@"%@",[[[self class] objcetMapping] objectForKey:keyPath]); + [_JSON setObject:[change objectForKey:@"new"] forKey:[[[self class] objcetMapping] objectForKey:keyPath]]; } -/*- (void)dealloc - { +- (void)dealloc +{ NSDictionary *properties = [PNObject propertiesForClass:self.class]; for (NSString *propertyName in properties) { - if ([[PNObject protectedProperties] containsObject:propertyName]) { - continue; - } - [self removeObserver:self forKeyPath:propertyName]; + if ([propertyName isEqualToString:@"description"] || [propertyName isEqualToString:@"debugDescription"]) { + continue; + } + [self removeObserver:self forKeyPath:propertyName]; } - } - */ + _JSON = nil; + _objectMapping = nil; + _objID = nil; + _createdAt = nil; + +} + @end diff --git a/Pod/Classes/PNObjectConfig.m b/Pod/Classes/PNObjectConfig.m index 76722c6..e61c3b5 100644 --- a/Pod/Classes/PNObjectConfig.m +++ b/Pod/Classes/PNObjectConfig.m @@ -39,134 +39,136 @@ static bool isFirstAccess = YES; + (instancetype)sharedInstance { - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - isFirstAccess = NO; - SINGLETON = [[super allocWithZone:NULL] init]; - }); - - return SINGLETON; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + isFirstAccess = NO; + SINGLETON = [[super allocWithZone:NULL] init]; + }); + + return SINGLETON; } #pragma mark - Life Cycle + (instancetype) initSharedInstanceForEnvironments:(NSDictionary *) endpointUrlsForEnvironments { - SINGLETON = [self sharedInstance]; - - if (SINGLETON) { - for (NSString *key in [endpointUrlsForEnvironments allKeys]) { - - ((void (^)())@{ - EnvironmentDevelopment : ^{ - NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]]; - if (endpointUrl) { - [SINGLETON.configuration setValue:[endpointUrl absoluteString] forKey:key]; - SINGLETON.devEnabled = YES; - } - - }, - EnvironmentStage : ^{ - NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]]; - if (endpointUrl) { - [SINGLETON.configuration setValue:[endpointUrl absoluteString] forKey:key]; - SINGLETON.stageEnabled = YES; - } - }, - EnvironmentProduction : ^{ - NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]]; - if (endpointUrl) { - [SINGLETON.configuration setValue:[endpointUrl absoluteString] forKey:key]; - SINGLETON.productionEnabled = YES; - } - } - }[key] ?: ^{ - - })(); - } - NSAssert(SINGLETON.productionEnabled, @"EnvironmentProduction must be valid endpoint url"); - SINGLETON.currentEnvironment = [[SINGLETON configuration] objectForKey:EnvironmentProduction]; - - } - return SINGLETON; + SINGLETON = [self sharedInstance]; + + if (SINGLETON) { + for (NSString *key in [endpointUrlsForEnvironments allKeys]) { + + ((void (^)())@{ + EnvironmentDevelopment : ^{ + NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]]; + if (endpointUrl) { + [SINGLETON.configuration setValue:[endpointUrl absoluteString] forKey:key]; + SINGLETON.devEnabled = YES; + } + + }, + EnvironmentStage : ^{ + NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]]; + if (endpointUrl) { + [SINGLETON.configuration setValue:[endpointUrl absoluteString] forKey:key]; + SINGLETON.stageEnabled = YES; + } + }, + EnvironmentProduction : ^{ + NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]]; + if (endpointUrl) { + [SINGLETON.configuration setValue:[endpointUrl absoluteString] forKey:key]; + SINGLETON.productionEnabled = YES; + } + } + }[key] ?: ^{ + + })(); + } + NSAssert(SINGLETON.productionEnabled, @"EnvironmentProduction must be valid endpoint url"); + SINGLETON.currentEnvironment = [[SINGLETON configuration] objectForKey:EnvironmentProduction]; + + } + return SINGLETON; } + (id) allocWithZone:(NSZone *)zone { - return [self sharedInstance]; + return [self sharedInstance]; } + (id)copyWithZone:(struct _NSZone *)zone { - return [self sharedInstance]; + return [self sharedInstance]; } + (id)mutableCopyWithZone:(struct _NSZone *)zone { - return [self sharedInstance]; + return [self sharedInstance]; } - (id)copy { - return [[PNObjectConfig alloc] init]; + return [[PNObjectConfig alloc] init]; } - (id)mutableCopy { - return [[PNObjectConfig alloc] init]; + return [[PNObjectConfig alloc] init]; } - (id) init { - if(SINGLETON){ - return SINGLETON; - } - if (isFirstAccess) { - [self doesNotRecognizeSelector:_cmd]; - } - self = [super init]; - - if (self) { - _configuration = [[NSMutableDictionary alloc] init]; - _minPasswordLenght = minPassLenght; - } - return self; + if(SINGLETON){ + return SINGLETON; + } + if (isFirstAccess) { + [self doesNotRecognizeSelector:_cmd]; + } + self = [super init]; + + if (self) { + _configuration = [[NSMutableDictionary alloc] init]; + _minPasswordLenght = minPassLenght; + + _manager = [AFHTTPSessionManager manager]; + } + return self; } - (void) setEnvironment:(Environment) env { - - _currentEnvironment = nil; - - switch (env) { - case Development:{ - if (_devEnabled) { - _currentEnvironment = [_configuration objectForKey:EnvironmentDevelopment]; - } - } - break; - case Stage:{ - if (_stageEnabled) { - _currentEnvironment = [_configuration objectForKey:EnvironmentStage]; - } - } - break; - case Production: - default: - if (_productionEnabled) { - _currentEnvironment = [_configuration objectForKey:EnvironmentProduction]; - } - break; - } - - NSAssert(_currentEnvironment,@"Selected environment generate error. Please check configuration"); - + + _currentEnvironment = nil; + + switch (env) { + case Development:{ + if (_devEnabled) { + _currentEnvironment = [_configuration objectForKey:EnvironmentDevelopment]; + } + } + break; + case Stage:{ + if (_stageEnabled) { + _currentEnvironment = [_configuration objectForKey:EnvironmentStage]; + } + } + break; + case Production: + default: + if (_productionEnabled) { + _currentEnvironment = [_configuration objectForKey:EnvironmentProduction]; + } + break; + } + + NSAssert(_currentEnvironment,@"Selected environment generate error. Please check configuration"); + } - (NSString *) PNObjEndpoint { - return _currentEnvironment; + return _currentEnvironment; } - (void) setAcceptablePasswordLenght:(NSUInteger) passLenght { - _minPasswordLenght = passLenght; + _minPasswordLenght = passLenght; } @end From c23b1e0e3810c14822ebbbf5f285abb92f356518 Mon Sep 17 00:00:00 2001 From: Giuseppe Nucifora Date: Wed, 3 Feb 2016 02:21:44 +0100 Subject: [PATCH 2/2] - Fix AccessToken Save, SharedInstance - Fix crash in removeObserver PNObject --- Example/PNObject/PNObjectAppDelegate.m | 18 ++-- Pod/Classes/PNClasses/PNAccessToken.h | 9 +- Pod/Classes/PNClasses/PNAccessToken.m | 93 +++++++++++++--- Pod/Classes/PNObject+PNObjectConnection.m | 126 +++++++++++----------- Pod/Classes/PNObject+Protected.m | 4 +- Pod/Classes/PNObject.h | 2 +- Pod/Classes/PNObject.m | 36 ++++--- Pod/Classes/PNObjectModel.m | 6 +- 8 files changed, 187 insertions(+), 107 deletions(-) diff --git a/Example/PNObject/PNObjectAppDelegate.m b/Example/PNObject/PNObjectAppDelegate.m index 3cfa1e2..15cbf2c 100644 --- a/Example/PNObject/PNObjectAppDelegate.m +++ b/Example/PNObject/PNObjectAppDelegate.m @@ -51,22 +51,28 @@ [user getJSONObject];*/ + PNAccessToken *accessToken = [PNAccessToken currentAccessToken]; + NSLog(@"%@",[accessToken JSONObject]); - [[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingString:[PNAccessToken PNObjClassName]] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { + /*[[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingString:[PNAccessToken PNObjClassName]] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { - } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { PNAccessToken *accessToken = [[PNAccessToken alloc] initWithJSON:responseObject]; - NSLog(@"%@",[accessToken getJSONObject]); - [accessToken saveLocally]; + NSLog(@"%@",[accessToken JSONObject]); - } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [accessToken saveLocally]; + }); + //[accessToken saveLocally]; + + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@",error); - }]; + }];*/ //NSLog(@"%@",userDictionary); /* diff --git a/Pod/Classes/PNClasses/PNAccessToken.h b/Pod/Classes/PNClasses/PNAccessToken.h index 4f5d51b..5250dde 100644 --- a/Pod/Classes/PNClasses/PNAccessToken.h +++ b/Pod/Classes/PNClasses/PNAccessToken.h @@ -20,9 +20,16 @@ typedef NS_ENUM(NSInteger, TokenType) { * * @return singleton */ -+ (instancetype _Nonnull) currentUser; ++ (instancetype _Nonnull) currentAccessToken; +/** + * gets singleton object of current user session. + * + * @return singleton + */ ++ (instancetype _Nonnull) currentAccessTokenWithJSON:(NSDictionary *)JSON; + ///-------------------------------------- #pragma mark - PNAccessToken Properties ///-------------------------------------- diff --git a/Pod/Classes/PNClasses/PNAccessToken.m b/Pod/Classes/PNClasses/PNAccessToken.m index 68a097e..8426244 100644 --- a/Pod/Classes/PNClasses/PNAccessToken.m +++ b/Pod/Classes/PNClasses/PNAccessToken.m @@ -15,6 +15,81 @@ @implementation PNAccessToken +static PNAccessToken *SINGLETON = nil; + +static bool isFirstAccess = YES; + +#pragma mark - Public Method + ++ (instancetype) currentAccessToken { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + isFirstAccess = NO; + + SINGLETON = [[super allocWithZone:NULL] initForCurrentAccessTokenWithJSON:nil]; + }); + + return SINGLETON; +} + ++ (instancetype _Nonnull) currentAccessTokenWithJSON:(NSDictionary *)JSON { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + isFirstAccess = NO; + + SINGLETON = [[super allocWithZone:NULL] initForCurrentAccessTokenWithJSON:JSON]; + }); + + return SINGLETON; +} + + +- (instancetype) initForCurrentAccessTokenWithJSON:(NSDictionary *)JSON { + if(SINGLETON){ + return SINGLETON; + } + if (isFirstAccess) { + [self doesNotRecognizeSelector:_cmd]; + } + NSDictionary *savedAccessToken; + + if (JSON) { + savedAccessToken = JSON; + } + else { + savedAccessToken = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]]; + } + + if (savedAccessToken) { + self = [super initWithJSON:savedAccessToken]; + } + else { + self = [super init]; + } + + if (self) { + if (_tokenTypeString) { + ((void (^)())@{ + @"beaer" : ^{ + _tokenType = TokenTypeBearer; + }, + @"basic" : ^{ + _tokenType = TokenTypeBasic; + } + }[_tokenTypeString] ?: ^{ + + })(); + } + + if (_expiresIn) { + _expirationDate = [[NSDate date] dateByAddingHours:[_expiresIn integerValue]]; + } + } + + return self; +} + + #pragma mark PNObjectSubclassing Protocol + (NSDictionary *)objcetMapping { @@ -30,23 +105,7 @@ } - (instancetype) initWithJSON:(NSDictionary *)JSON { - self = [super initWithJSON:JSON]; - - if (self) { - ((void (^)())@{ - @"beaer" : ^{ - _tokenType = TokenTypeBearer; - }, - @"basic" : ^{ - _tokenType = TokenTypeBasic; - } - }[_tokenTypeString] ?: ^{ - - })(); - - _expirationDate = [[NSDate date] dateByAddingHours:[_expiresIn integerValue]]; - } - return self; + return [[self class] currentAccessTokenWithJSON:JSON]; } + (NSString *)objectClassName { diff --git a/Pod/Classes/PNObject+PNObjectConnection.m b/Pod/Classes/PNObject+PNObjectConnection.m index 2b21658..677b20c 100644 --- a/Pod/Classes/PNObject+PNObjectConnection.m +++ b/Pod/Classes/PNObject+PNObjectConnection.m @@ -19,79 +19,79 @@ - (NSURLSessionDataTask * _Nonnull)GETWithProgress:(void (^ _Nullable)(NSProgress * _Nonnull))downloadProgress - success:(void (^ _Nullable)(NSURLSessionDataTask * _Nonnull, id _Nullable))success - failure:(void (^ _Nullable)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure { - - return [[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) { - - - NSLogDebug(@"JSON: %@", responseObject); - NSLogDebug(@"JSON: %@", [responseObject class]); - - self.JSON = [[NSDictionary alloc] initWithDictionary:responseObject]; - - [self populateObjectFromJSON:responseObject]; - - - } failure:^(NSURLSessionTask *operation, NSError *error) { - - NSLogDebug(@"Error: %@", error); - - }]; + success:(void (^ _Nullable)(NSURLSessionDataTask * _Nonnull, id _Nullable))success + failure:(void (^ _Nullable)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure { + + return [[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) { + + + NSLogDebug(@"JSON: %@", responseObject); + NSLogDebug(@"JSON: %@", [responseObject class]); + + self.JSON = [[NSDictionary alloc] initWithDictionary:responseObject]; + + [self populateObjectFromJSON:responseObject]; + + + } failure:^(NSURLSessionTask *operation, NSError *error) { + + NSLogDebug(@"Error: %@", error); + + }]; } - (NSURLSessionDataTask *)POSTWithProgress:(nullable void (^)(NSProgress * _Nonnull)) uploadProgress - success:(void (^)(NSURLSessionDataTask *task, id responseObject))success - failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { - - return [[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] - parameters:[self getJSONObject] constructingBodyWithBlock:^(id _Nonnull formData) { - - } progress:^(NSProgress * _Nonnull _uploadProgress) { - if (uploadProgress) { - uploadProgress(_uploadProgress); - } - } success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) { - if (success) { - success(_task,_responseObject); - } - } failure:^(NSURLSessionDataTask * _Nullable _task, NSError * _Nonnull _error) { - if (failure) { - failure(_task,_error); - } - }]; + success:(void (^)(NSURLSessionDataTask *task, id responseObject))success + failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { + + return [[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] + parameters:[self JSONObject] constructingBodyWithBlock:^(id _Nonnull formData) { + + } progress:^(NSProgress * _Nonnull _uploadProgress) { + if (uploadProgress) { + uploadProgress(_uploadProgress); + } + } success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) { + if (success) { + success(_task,_responseObject); + } + } failure:^(NSURLSessionDataTask * _Nullable _task, NSError * _Nonnull _error) { + if (failure) { + failure(_task,_error); + } + }]; } - (NSURLSessionDataTask *)PUTWithSuccess:(void (^)(NSURLSessionDataTask *task, id responseObject))success - failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { - - return [[[PNObjectConfig sharedInstance] manager] PUT:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] - parameters:[self getJSONObject] - success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) { - if (success) { - success(_task,_responseObject); - } - } failure:^(NSURLSessionDataTask * _Nullable _task, NSError * _Nonnull _error) { - if (failure) { - failure(_task,_error); - } - }]; + failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { + + return [[[PNObjectConfig sharedInstance] manager] PUT:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] + parameters:[self JSONObject] + success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) { + if (success) { + success(_task,_responseObject); + } + } failure:^(NSURLSessionDataTask * _Nullable _task, NSError * _Nonnull _error) { + if (failure) { + failure(_task,_error); + } + }]; } - (NSURLSessionDataTask *)DELETEWithSuccess:(void (^)(NSURLSessionDataTask *task, id responseObject))success - failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { - return [[[PNObjectConfig sharedInstance] manager] DELETE:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] - parameters:[self getJSONObject] - success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) { - if (success) { - success(_task,_responseObject); - } - } failure:^(NSURLSessionDataTask * _Nullable _task, NSError * _Nonnull _error) { - if (failure) { - failure(_task,_error); - } - }]; + failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { + return [[[PNObjectConfig sharedInstance] manager] DELETE:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] + parameters:[self JSONObject] + success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) { + if (success) { + success(_task,_responseObject); + } + } failure:^(NSURLSessionDataTask * _Nullable _task, NSError * _Nonnull _error) { + if (failure) { + failure(_task,_error); + } + }]; } @end diff --git a/Pod/Classes/PNObject+Protected.m b/Pod/Classes/PNObject+Protected.m index 4bc7c8d..83ee3e3 100644 --- a/Pod/Classes/PNObject+Protected.m +++ b/Pod/Classes/PNObject+Protected.m @@ -42,7 +42,7 @@ NSString *mappedJSONType; NSString *propertyType = [properties valueForKey:propertyName]; - NSLogDebug(@"%@",self.objectMapping); + NSLogDebug(@"%@",self.JSONObject); NSLogDebug(@"%@",[[self class] objcetMapping]); @@ -156,7 +156,7 @@ NSString *propertyType = [properties valueForKey:propertyName]; - id mappingValue = [self.objectMapping valueForKey:propertyName]; + id mappingValue = [self.JSONObject valueForKey:propertyName]; if([mappingValue isKindOfClass:NSDictionary.class]) { mappedJSONKey = [mappingValue valueForKey:@"key"]; diff --git a/Pod/Classes/PNObject.h b/Pod/Classes/PNObject.h index 0ca80f8..538a03f 100644 --- a/Pod/Classes/PNObject.h +++ b/Pod/Classes/PNObject.h @@ -50,7 +50,7 @@ /** * <#Description#> */ -@property (nonatomic, strong, getter=getJSONObject, nonnull) NSDictionary * objectMapping; +@property (nonatomic, strong, nonnull) NSDictionary * JSONObject; @end diff --git a/Pod/Classes/PNObject.m b/Pod/Classes/PNObject.m index f6392dd..1cf63d5 100644 --- a/Pod/Classes/PNObject.m +++ b/Pod/Classes/PNObject.m @@ -75,9 +75,9 @@ NSMutableDictionary * objectDict = [[NSMutableDictionary alloc] initWithDictionary:[[self class] objcetMapping]]; [objectDict addEntriesFromDictionary:[PNObject objcetMapping]]; - _objectMapping = objectDict; + _JSONObject = objectDict; - NSAssert(_objectMapping, @"You must create objectMapping"); + NSAssert(_JSONObject, @"You must create objectMapping"); _singleInstance = [[self class] singleInstance]; @@ -102,9 +102,9 @@ NSMutableDictionary * objectDict = [[NSMutableDictionary alloc] initWithDictionary:[[self class] objcetMapping]]; [objectDict addEntriesFromDictionary:[PNObject objcetMapping]]; - _objectMapping = objectDict; + _JSONObject = objectDict; - NSAssert(_objectMapping, @"You must create objectMapping"); + NSAssert(_JSONObject, @"You must create objectMapping"); _singleInstance = [[self class] singleInstance]; @@ -112,7 +112,7 @@ } - NSAssert(_objectMapping, @"You must create objectMapping"); + NSAssert(_JSONObject, @"You must create objectMapping"); _JSON = [[NSMutableDictionary alloc] initWithDictionary:JSON]; [self populateObjectFromJSON:_JSON]; @@ -129,8 +129,8 @@ NSDictionary *properties = [PNObject propertiesForClass:self.class]; - for (NSString* propertyName in _objectMapping) { - id mappingValue = [_objectMapping objectForKey:propertyName]; + for (NSString* propertyName in _JSONObject) { + id mappingValue = [_JSONObject objectForKey:propertyName]; if([mappingValue isKindOfClass:NSDictionary.class]) { mappedJSONKey = [mappingValue valueForKey:@"key"]; @@ -205,7 +205,7 @@ @"NSArray" : ^{ NSMutableArray *arr = [NSMutableArray array]; for(id PNObject in value) { - SEL selector = NSSelectorFromString(@"getJSONObject"); + SEL selector = NSSelectorFromString(@"JSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: [[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:PNObject]; @@ -224,7 +224,7 @@ PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject]; [arr addObject:val]; - SEL selector = NSSelectorFromString(@"getJSONObject"); + SEL selector = NSSelectorFromString(@"JSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:value]; @@ -238,7 +238,7 @@ }[propertyType] ?: ^{ BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]]; if(isPNObjectSubclass) { - SEL selector = NSSelectorFromString(@"getJSONObject"); + SEL selector = NSSelectorFromString(@"JSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:value]; @@ -260,7 +260,7 @@ } -- (NSDictionary* _Nonnull) getJSONObject { +- (NSDictionary* _Nonnull) JSONObject { if (!_JSON) { return [self reverseMapping]; } @@ -321,7 +321,6 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - NSLogDebug(@"%@",[[[self class] objcetMapping] objectForKey:keyPath]); [_JSON setObject:[change objectForKey:@"new"] forKey:[[[self class] objcetMapping] objectForKey:keyPath]]; } @@ -333,10 +332,19 @@ if ([propertyName isEqualToString:@"description"] || [propertyName isEqualToString:@"debugDescription"]) { continue; } - [self removeObserver:self forKeyPath:propertyName]; + + @try { + [self removeObserver:self forKeyPath:propertyName]; + } + @catch (NSException *exception) { + + } + @finally { + + } } _JSON = nil; - _objectMapping = nil; + _JSONObject = nil; _objID = nil; _createdAt = nil; diff --git a/Pod/Classes/PNObjectModel.m b/Pod/Classes/PNObjectModel.m index 41e7f7c..1fa78b7 100644 --- a/Pod/Classes/PNObjectModel.m +++ b/Pod/Classes/PNObjectModel.m @@ -161,7 +161,7 @@ static bool isFirstAccess = YES; if ([(PNObject*) object singleInstance]) { - SEL selector = NSSelectorFromString(@"getJSONObject"); + SEL selector = NSSelectorFromString(@"JSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:object]; @@ -197,7 +197,7 @@ static bool isFirstAccess = YES; NSMutableArray *objects = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]]; - SEL selector = NSSelectorFromString(@"getJSONObject"); + SEL selector = NSSelectorFromString(@"JSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:object]; @@ -225,7 +225,7 @@ static bool isFirstAccess = YES; NSMutableArray *objects = [[NSMutableArray alloc] init]; - SEL selector = NSSelectorFromString(@"getJSONObject"); + SEL selector = NSSelectorFromString(@"JSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:object];