- Important fix in PNObject

This commit is contained in:
Giuseppe Nucifora 2016-01-27 17:07:06 +01:00
parent fad171373e
commit 7fc6f135bd
7 changed files with 205 additions and 81 deletions

View File

@ -15,88 +15,92 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[PNObjectConfig initSharedInstanceForEnvironments:@{ EnvironmentDevelopment : @"https://development.it/api/v1",
EnvironmentStage : @"https://stage.it/api/v1",
EnvironmentProduction : @"http://pnobjectdemo.giuseppenucifora.com/"
}];
[[PNObjectConfig sharedInstance] setEnvironment:Production];
[[[PNObjectConfig sharedInstance] manager] setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate]];
[[PNObjectConfig sharedInstance] manager].securityPolicy.allowInvalidCertificates = YES;
//[PNObject get];*/
PNUser *user = [PNUser currentUser];
NSLog(@"user : %@",[user getJSONObject]);
//[user autoRemoveLocally];
/*[user setFirstName:@"Giuseppe2"];
[user setLastName:@"Nucifora2"];
[user setEmail:@"giuseppe.nucifora@giuseppenucifora.com"];
[user setSex:@"M"];
[user setHasAcceptedNewsletter:NO];
[user setHasAcceptedPrivacy:YES];
[user setUsername:@"giuseppe.nucifora"];
[user setPassword:@"giuseppe.nucifora.password"];
[user setPhone:@"+393485904995"];
[user setUserId:@"blablabla"];
[user saveLocally];
*/
//NSLog(@"%@",[user getObject]);
/*
PNAddress *address1 = [[PNAddress alloc] init];
[address1 setZip:@"95014"];
[address1 setCountry:@"Italy"];
[address1 setCity:@"Giarre"];
[address1 setProvince:@"Catania"];
NSLog(@"%@",[address1 saveLocally]);
PNAddress *address2 = [[PNAddress alloc] init];
[address2 setZip:@"95014"];
[address2 setCountry:@"Italy"];
[address2 setCity:@"Giarre"];
[address2 setProvince:@"Catania"];
NSLog(@"%@",[address2 saveLocally]);*/
return YES;
// Override point for customization after application launch.
[PNObjectConfig initSharedInstanceForEnvironments:@{ EnvironmentDevelopment : @"https://development.it/api/v1",
EnvironmentStage : @"https://stage.it/api/v1",
EnvironmentProduction : @"http://pnobjectdemo.giuseppenucifora.com/"
}];
[[PNObjectConfig sharedInstance] setEnvironment:Production];
[[[PNObjectConfig sharedInstance] manager] setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate]];
[[PNObjectConfig sharedInstance] manager].securityPolicy.allowInvalidCertificates = YES;
//[PNObject get];*/
PNUser *user = [PNUser currentUser];
//NSLog(@"user : %@",[user getJSONObject]);
//[user autoRemoveLocally];
[user setFirstName:@"Giuseppe2"];
[user setLastName:@"Nucifora2"];
[user setEmail:@"giuseppe.nucifora@giuseppenucifora.com"];
[user setSex:@"M"];
[user setHasAcceptedNewsletter:NO];
[user setHasAcceptedPrivacy:YES];
[user setUsername:@"giuseppe.nucifora"];
[user setPassword:@"giuseppe.nucifora.password"];
[user setPhone:@"+393485904995"];
[user setUserId:@"blablabla"];
[user saveLocally];
[user setFirstName:@"Angela"];
[user setLastName:@"Sapienza"];
[user getJSONObject];
//NSLog(@"%@",userDictionary);
/*
PNAddress *address1 = [[PNAddress alloc] init];
[address1 setZip:@"95014"];
[address1 setCountry:@"Italy"];
[address1 setCity:@"Giarre"];
[address1 setProvince:@"Catania"];
NSLog(@"%@",[address1 saveLocally]);
PNAddress *address2 = [[PNAddress alloc] init];
[address2 setZip:@"95014"];
[address2 setCountry:@"Italy"];
[address2 setCity:@"Giarre"];
[address2 setProvince:@"Catania"];
NSLog(@"%@",[address2 saveLocally]);*/
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end

View File

@ -17,8 +17,18 @@
* @return singleton
*/
+ (instancetype _Nonnull) currentUser;
/**
* <#Description#>
*
* @param password <#password description#>
*
* @return <#return value description#>
*/
- (BOOL) isValidPassword:(NSString* _Nonnull) password;
/**
* <#Description#>
*/
- (void) logout;
//- (void) setPassword:(NSString * _Nonnull)password inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id responseObject, NSError * error)) responseBlock;
@ -29,15 +39,15 @@
/**
* <#Description#>
*/
@property (strong, nonatomic, nonnull) NSString * userId;
@property (strong, nonatomic, nullable) NSString * userId;
/**
* <#Description#>
*/
@property (strong, nonatomic, nonnull) NSString * firstName;
@property (strong, nonatomic, nullable) NSString * firstName;
/**
* <#Description#>
*/
@property (strong, nonatomic, nonnull) NSString * lastName;
@property (strong, nonatomic, nullable) NSString * lastName;
/**
* <#Description#>
*/
@ -73,15 +83,15 @@
/**
* <#Description#>
*/
@property (nonatomic, strong, nonnull) NSString * email;
@property (nonatomic, strong, nullable) NSString * email;
/**
* <#Description#>
*/
@property (nonatomic, strong, nonnull) NSString * username;
@property (nonatomic, strong, nullable) NSString * username;
/**
* <#Description#>
*/
@property (nonatomic, strong, nonnull) NSString * password;
@property (nonatomic, strong, nullable) NSString * password;
/**
* <#Description#>
*/
@ -89,7 +99,7 @@
/**
* <#Description#>
*/
@property (nonatomic, strong, nonnull) NSNumber * loginCount;
@property (nonatomic, strong, nullable) NSNumber * loginCount;
/**
* <#Description#>
*/

View File

@ -91,7 +91,9 @@ static bool isFirstAccess = YES;
if ([email isValidEmail]) {
_email = email;
}
NSLogDebug(@"insertedEmail is not valid.");
else {
NSLogDebug(@"insertedEmail is not valid.");
}
}
- (void) setPassword:(NSString *)password {
@ -115,6 +117,11 @@ static bool isFirstAccess = YES;
return @"password is not readble";
}
- (void)logout {
[self autoRemoveLocally];
[self resetObject];
}
#pragma mark PNObjectSubclassing Protocol
+ (NSDictionary *)objcetMapping {

View File

@ -38,6 +38,7 @@
*/
+ (NSDictionary * _Nullable)propertiesForClass:(Class _Nonnull)PNObjClass;
- (void)resetObject;
///--------------------------------------
#pragma mark - PNObject (Protected) Properties

View File

@ -26,6 +26,10 @@
{
NSDictionary *properties = [PNObject propertiesForClass:self.class];
for (NSString *propertyName in properties) {
[self addObserver:self forKeyPath:propertyName options:NSKeyValueObservingOptionNew context:nil];
}
for(NSString *propertyName in properties) {
if([propertyName isEqualToString:@"mappingError"])
@ -132,6 +136,94 @@
}
}
- (void)resetObject
{
NSDictionary *properties = [PNObject propertiesForClass:self.class];
for(NSString *propertyName in properties) {
if([propertyName isEqualToString:@"mappingError"])
continue;
NSString *mappedJSONKey;
NSString *mappedJSONType;
NSString *propertyType = [properties valueForKey:propertyName];
id mappingValue = [self.objectMapping valueForKey:propertyName];
if([mappingValue isKindOfClass:NSDictionary.class]) {
mappedJSONKey = [mappingValue valueForKey:@"key"];
mappedJSONType = [mappingValue valueForKey:@"type"];
} else {
mappedJSONKey = mappingValue;
}
if ([[PNObject protectedProperties] containsObject:propertyName]
|| [propertyName isEqualToString:@"description"]
|| [propertyName isEqualToString:@"debugDescription"]) {
continue;
}
// Get JSON value for the mapped key
((void (^)())@{
@"c" : ^{
char val = '\0';
[self setValue:@(val) forKey:propertyName];
},
@"d" : ^{
double val = 0.0;
[self setValue:@(val) forKey:propertyName];
},
@"f" : ^{
float val = 0.0;
[self setValue:@(val) forKey:propertyName];
},
@"i" : ^{
int val = 0;
[self setValue:@(val) forKey:propertyName];
},
@"l" : ^{
long val = 0;
[self setValue:@(val) forKey:propertyName];
},
@"s" : ^{
short val = 0;
[self setValue:@(val) forKey:propertyName];
},
@"B" : ^{
[self setValue:@(NO) forKey:propertyName];
},
@"NSString" : ^{
[self setValue:[[NSString alloc] init] forKey:propertyName];
},
@"NSNumber" : ^{
[self setValue:[[NSNumber alloc] init] forKey:propertyName];
},
@"NSDate" : ^{
[self setValue:[[NSDate alloc] init] forKey:propertyName];
},
@"NSArray" : ^{
[self setValue:[[NSArray alloc] init] forKey:propertyName];
},
@"NSMutableArray" : ^{
[self setValue:[[NSMutableArray alloc] init] forKey:propertyName];
}
}[propertyType] ?: ^{
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
[self setValue:@"" forKey:propertyName];
}
else {
NSString *errorStr = [NSString stringWithFormat:@"Property '%@' could not be assigned any value.", propertyName];
NSLogDebug(@"%@",errorStr);
}
})();
}
}
- (BOOL)isObjNull:(id _Nullable)obj
{
if(!obj || nil == obj || NSNull.null == obj || ([obj isKindOfClass:[NSString class]] && [obj isEqualToString:@"(null)"]) || [obj isEqual:[NSNull null]])

View File

@ -12,6 +12,7 @@
#import "PNObject/PNUser.h"
#import "PNObjectConstants.h"
#import "PNObject+Protected.h"
#import "objc/runtime.h"
#define PNOBJECT_DIR @"PNObjects"
@ -242,10 +243,6 @@
// do nothing
}
})();
}
_JSON = JSON;
@ -254,12 +251,17 @@
}
- (NSDictionary* _Nonnull) getJSONObject {
return [self reverseMapping];
if (!_JSON) {
return [self reverseMapping];
}
else {
return _JSON;
}
}
- (NSString* _Nonnull) description {
if (!_JSON) {
return [[self reverseMapping] description];
[self reverseMapping];
}
return [_JSON description];
}
@ -305,4 +307,16 @@
#pragma mark -
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[_JSON setObject:[change objectForKey:@"new"] forKey:keyPath];
}
- (void)dealloc
{
unsigned int propertyCount;
objc_property_t *properties = class_copyPropertyList([self class], &propertyCount);
for(int i = 0; i < propertyCount; i++)[self removeObserver:self forKeyPath:[NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding]];
}
@end

View File

@ -170,8 +170,6 @@ static bool isFirstAccess = YES;
NSDictionary *objectDict;
[invocation getReturnValue:&objectDict];
NSLogDebug(@"%@",objectDict);
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objectDict];
if ([self issetPNObjectModelForObject:object]) {
@ -236,8 +234,6 @@ static bool isFirstAccess = YES;
NSDictionary *objectDict;
[invocation getReturnValue:&objectDict];
NSLogDebug(@"%@",objectDict);
[objects addObject:objectDict];
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects];