- Fix SharedInstance User

- Fix Object save Locally
- Fix Object fetch Locally
- Add Object delete Locally
This commit is contained in:
Giuseppe Nucifora 2016-01-26 12:26:49 +01:00
parent 7e5a6149a8
commit 4c97b281ed
9 changed files with 81 additions and 48 deletions

View File

@ -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]);
/*

View File

@ -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?

View File

@ -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<AFMultipartFormData> _Nonnull formData) {
parameters:[self getJSONObject] constructingBodyWithBlock:^(id<AFMultipartFormData> _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);

View File

@ -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

View File

@ -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<PNObjectSubclassing> _Nonnull subClassDelegate;
- (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON;
- (id _Nonnull) saveLocally;
- (BOOL) autoRemoveLocally;
@end

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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;
}