- 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

@ -29,11 +29,10 @@
PNUser *user = [PNUser currentUser];
NSLog(@"user : %@",[user getJSONObject]);
//NSLog(@"user : %@",[user getJSONObject]);
//[user autoRemoveLocally];
/*[user setFirstName:@"Giuseppe2"];
[user setFirstName:@"Giuseppe2"];
[user setLastName:@"Nucifora2"];
[user setEmail:@"giuseppe.nucifora@giuseppenucifora.com"];
[user setSex:@"M"];
@ -44,9 +43,14 @@
[user setPhone:@"+393485904995"];
[user setUserId:@"blablabla"];
[user saveLocally];
*/
//NSLog(@"%@",[user getObject]);
[user setFirstName:@"Angela"];
[user setLastName:@"Sapienza"];
[user getJSONObject];
//NSLog(@"%@",userDictionary);
/*

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,8 +91,10 @@ static bool isFirstAccess = YES;
if ([email isValidEmail]) {
_email = email;
}
else {
NSLogDebug(@"insertedEmail is not valid.");
}
}
- (void) setPassword:(NSString *)password {
if ([self isValidPassword: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 {
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];