From 4c97b281ed1b8c189c1ff40395c4823822268fe6 Mon Sep 17 00:00:00 2001 From: Giuseppe Nucifora Date: Tue, 26 Jan 2016 12:26:49 +0100 Subject: [PATCH] - Fix SharedInstance User - Fix Object save Locally - Fix Object fetch Locally - Add Object delete Locally --- Example/PNObject/PNObjectAppDelegate.m | 14 +++++--- PNObject.podspec | 4 +-- Pod/Classes/PNObject+PNObjectConnection.m | 6 ++-- Pod/Classes/PNObject+Protected.m | 21 +++++------ Pod/Classes/PNObject.h | 4 ++- Pod/Classes/PNObject.m | 44 ++++++++++++++++++----- Pod/Classes/PNObjectModel.h | 2 +- Pod/Classes/PNObjectModel.m | 16 ++++----- Pod/Classes/User/PNUser.m | 18 +++++----- 9 files changed, 81 insertions(+), 48 deletions(-) diff --git a/Example/PNObject/PNObjectAppDelegate.m b/Example/PNObject/PNObjectAppDelegate.m index 3db6643..45c6c18 100644 --- a/Example/PNObject/PNObjectAppDelegate.m +++ b/Example/PNObject/PNObjectAppDelegate.m @@ -30,16 +30,22 @@ PNUser *user = [PNUser currentUser]; - NSLog(@"asd"); - /*[user setFirstName:@"Giuseppe"]; - [user setLastName:@"Nucifora"]; - [user setHasAcceptedNewsletter:YES]; + 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]); /* diff --git a/PNObject.podspec b/PNObject.podspec index 8d81489..982fe81 100644 --- a/PNObject.podspec +++ b/PNObject.podspec @@ -8,8 +8,8 @@ Pod::Spec.new do |s| s.name = "PNObject" -s.version = "0.1.0" -s.summary = "PNObject." +s.version = "0.2.0" +s.summary = "PNObject is a simple replica of the more complex ParseObject" # This description is used to generate tags and improve search results. # * Think: What does it do? Why did you write it? What is the focus? diff --git a/Pod/Classes/PNObject+PNObjectConnection.m b/Pod/Classes/PNObject+PNObjectConnection.m index d42d8a3..2b21658 100644 --- a/Pod/Classes/PNObject+PNObjectConnection.m +++ b/Pod/Classes/PNObject+PNObjectConnection.m @@ -46,7 +46,7 @@ failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { return [[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] - parameters:[self getObject] constructingBodyWithBlock:^(id _Nonnull formData) { + parameters:[self getJSONObject] constructingBodyWithBlock:^(id _Nonnull formData) { } progress:^(NSProgress * _Nonnull _uploadProgress) { if (uploadProgress) { @@ -67,7 +67,7 @@ failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure { return [[[PNObjectConfig sharedInstance] manager] PUT:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",[[self class] objectClassName]] - parameters:[self getObject] + parameters:[self getJSONObject] success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) { if (success) { success(_task,_responseObject); @@ -82,7 +82,7 @@ - (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 getObject] + parameters:[self getJSONObject] success:^(NSURLSessionDataTask * _Nonnull _task, id _Nullable _responseObject) { if (success) { success(_task,_responseObject); diff --git a/Pod/Classes/PNObject+Protected.m b/Pod/Classes/PNObject+Protected.m index 7a12df5..715df34 100644 --- a/Pod/Classes/PNObject+Protected.m +++ b/Pod/Classes/PNObject+Protected.m @@ -18,8 +18,12 @@ @dynamic JSON; @dynamic singleInstance; ++ (PNObjectModel* _Nonnull) objectModel { + +} + + (NSArray * _Nonnull) protectedProperties { - return @[@"JSON",@"subClassDelegate",@"objectModel",@"objectMapping"]; + return @[@"JSON",@"subClassDelegate",@"objectModel",@"objectMapping",@"singleInstance"]; } - (void)populateObjectFromJSON:(id _Nullable)JSON @@ -45,18 +49,13 @@ mappedJSONKey = mappingValue; } - // Check if there is mapping for the property - if([self isObjNull:mappedJSONKey]) { - // No mapping so just continue + + if ([[PNObject protectedProperties] containsObject:propertyName]) { continue; } - // Get JSON value for the mapped key id value = [JSON valueForKeyPath:propertyName]; - if([self isObjNull:value]) { - continue; - } ((void (^)())@{ @@ -89,9 +88,8 @@ [self setValue:@(val) forKey:propertyName]; }, @"NSString" : ^{ - NSString *val = [NSString stringWithFormat:@"%@", value]; - if (![self isObjNull:val]) { - [self setValue:val forKey:propertyName]; + if (![self isObjNull:value]) { + [self setValue:value forKey:propertyName]; } }, @@ -136,7 +134,6 @@ } })(); } - } - (BOOL)isObjNull:(id _Nullable)obj diff --git a/Pod/Classes/PNObject.h b/Pod/Classes/PNObject.h index 7fd101c..a6ab4ef 100644 --- a/Pod/Classes/PNObject.h +++ b/Pod/Classes/PNObject.h @@ -28,11 +28,13 @@ @property (nonatomic, strong) NSString * _Nonnull objID; @property (nonatomic, strong) NSDate * _Nonnull createdAt; -@property (nonatomic, strong, getter=getObject) NSDictionary * _Nonnull objectMapping; +@property (nonatomic, strong, getter=getJSONObject) NSDictionary * _Nonnull objectMapping; @property (nonatomic, assign) id _Nonnull subClassDelegate; - (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON; - (id _Nonnull) saveLocally; +- (BOOL) autoRemoveLocally; + @end diff --git a/Pod/Classes/PNObject.m b/Pod/Classes/PNObject.m index 6d7fdc3..ac955e0 100644 --- a/Pod/Classes/PNObject.m +++ b/Pod/Classes/PNObject.m @@ -43,10 +43,14 @@ if (self) { if ([[self class] isSubclassOfClass:[PNObject class]]) { + NSAssert([[self class] conformsToProtocol:@protocol(PNObjectSubclassing)], @"Subclass object must conform to PNObjectSubclassing"); _objID = [[NSProcessInfo processInfo] globallyUniqueString]; + _objectModel = [PNObjectModel sharedInstance]; + [_objectModel setPersistencyDelegate:self]; + NSMutableDictionary * objectDict = [[NSMutableDictionary alloc] initWithDictionary:[[self class] objcetMapping]]; [objectDict addEntriesFromDictionary:[self PNObjectMapping]]; @@ -56,23 +60,41 @@ _singleInstance = [[self class] singleInstance]; - _objectModel = [PNObjectModel sharedInstance]; - [_objectModel setPersistencyDelegate:self]; - _createdAt = [[NSDate date] toLocalTime]; + } } return self; } - (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON { - self = [self init]; + self = [super init]; if (self) { + if ([[self class] isSubclassOfClass:[PNObject class]]) { + NSAssert([[self class] conformsToProtocol:@protocol(PNObjectSubclassing)], @"Subclass object must conform to PNObjectSubclassing"); + + _objID = [[NSProcessInfo processInfo] globallyUniqueString]; + + _objectModel = [PNObjectModel sharedInstance]; + [_objectModel setPersistencyDelegate:self]; + + NSMutableDictionary * objectDict = [[NSMutableDictionary alloc] initWithDictionary:[[self class] objcetMapping]]; + [objectDict addEntriesFromDictionary:[self PNObjectMapping]]; + + _objectMapping = objectDict; + + NSAssert(_objectMapping, @"You must create objectMapping"); + + _singleInstance = [[self class] singleInstance]; + + _createdAt = [[NSDate date] toLocalTime]; + + } NSAssert(_objectMapping, @"You must create objectMapping"); _JSON = [[NSMutableDictionary alloc] initWithDictionary:JSON]; - [self populateObjectFromJSON:JSON]; + [self populateObjectFromJSON:_JSON]; } return self; } @@ -162,7 +184,7 @@ @"NSArray" : ^{ NSMutableArray *arr = [NSMutableArray array]; for(id PNObject in value) { - SEL selector = NSSelectorFromString(@"getObject"); + SEL selector = NSSelectorFromString(@"getJSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: [[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:PNObject]; @@ -181,7 +203,7 @@ PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject]; [arr addObject:val]; - SEL selector = NSSelectorFromString(@"getObject"); + SEL selector = NSSelectorFromString(@"getJSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:value]; @@ -195,7 +217,7 @@ }[propertyType] ?: ^{ BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]]; if(isPNObjectSubclass) { - SEL selector = NSSelectorFromString(@"getObject"); + SEL selector = NSSelectorFromString(@"getJSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:value]; @@ -220,7 +242,7 @@ return _JSON; } -- (NSDictionary* _Nonnull) getObject { +- (NSDictionary* _Nonnull) getJSONObject { return [self reverseMapping]; } @@ -263,6 +285,10 @@ return [_objectModel saveLocally:self]; } +- (BOOL) autoRemoveLocally { + return [_objectModel removeObjectLocally:self]; +} + #pragma mark - @end diff --git a/Pod/Classes/PNObjectModel.h b/Pod/Classes/PNObjectModel.h index 054030b..e3c4be0 100644 --- a/Pod/Classes/PNObjectModel.h +++ b/Pod/Classes/PNObjectModel.h @@ -39,7 +39,7 @@ * * @return <#return value description#> */ -- (id _Nonnull) removeObjectAndSaveLocally:(id _Nonnull) object; +- (BOOL) removeObjectLocally:(id _Nonnull) object; - (id _Nonnull) fetchObjectsWithClass:(Class _Nonnull) class; diff --git a/Pod/Classes/PNObjectModel.m b/Pod/Classes/PNObjectModel.m index 9d14595..6850e55 100644 --- a/Pod/Classes/PNObjectModel.m +++ b/Pod/Classes/PNObjectModel.m @@ -44,7 +44,6 @@ static bool isFirstAccess = YES; if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) { - NSLogDebug(@"%@",[object subClassDelegate]); NSString *className; //if ([[object subClassDelegate] respondsToSelector:@selector(objectClassName)]) { @@ -119,6 +118,7 @@ static bool isFirstAccess = YES; _fileManager = [PEARFileManager sharedInstatnce]; [_fileManager setRootDirectory:k_ROOT_DIR_DOCUMENTS]; + NSLogDebug(@"%@",[_fileManager getRootDirectoryPath]); } return self; } @@ -160,7 +160,7 @@ static bool isFirstAccess = YES; if ([(PNObject*) object singleInstance]) { - SEL selector = NSSelectorFromString(@"getObject"); + SEL selector = NSSelectorFromString(@"getJSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:object]; @@ -198,7 +198,7 @@ static bool isFirstAccess = YES; NSMutableArray *objects = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]]; - SEL selector = NSSelectorFromString(@"getObject"); + SEL selector = NSSelectorFromString(@"getJSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:object]; @@ -226,7 +226,7 @@ static bool isFirstAccess = YES; NSMutableArray *objects = [[NSMutableArray alloc] init]; - SEL selector = NSSelectorFromString(@"getObject"); + SEL selector = NSSelectorFromString(@"getJSONObject"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:object]; @@ -259,19 +259,19 @@ static bool isFirstAccess = YES; } } -- (id _Nonnull) removeObjectAndSaveLocally:(id _Nonnull) object { +- (BOOL) removeObjectLocally:(id _Nonnull) object { BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]]; if(isPNObjectSubclass) { if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) { - if ([(PNObject*) object singleInstance]) { - - + if ([self issetPNObjectModelForObject:object]) { + return [_fileManager deletePath:[self objectName:object]]; } } } + return NO; } @end diff --git a/Pod/Classes/User/PNUser.m b/Pod/Classes/User/PNUser.m index 447f2c9..f493573 100644 --- a/Pod/Classes/User/PNUser.m +++ b/Pod/Classes/User/PNUser.m @@ -77,15 +77,17 @@ static bool isFirstAccess = YES; if (isFirstAccess) { [self doesNotRecognizeSelector:_cmd]; } - self = [super init]; + + NSDictionary *savedUser = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]]; + + if (savedUser) { + self = [super initWithJSON:savedUser]; + } + else { + self = [super init]; + } + if (self) { - [self setSubClassDelegate:self]; - - super.JSON = [super.objectModel fetchObjectsWithClass:[self class]]; - - if(super.JSON){ - [super populateObjectFromJSON:super.JSON]; - } } return self; }