Skip to content

Commit

Permalink
Moving background JSON process methods to NSObject category from UIVi…
Browse files Browse the repository at this point in the history
…ewController category.
  • Loading branch information
devinross committed Feb 12, 2013
1 parent 6e6a37e commit 61a4706
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 223 deletions.
8 changes: 0 additions & 8 deletions src/TapkuLibrary.xcodeproj/project.pbxproj
Expand Up @@ -107,8 +107,6 @@
3BD32972125B753900BE5EAF /* TKAlertCenter.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BD32970125B753900BE5EAF /* TKAlertCenter.h */; }; 3BD32972125B753900BE5EAF /* TKAlertCenter.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BD32970125B753900BE5EAF /* TKAlertCenter.h */; };
3BD32973125B753900BE5EAF /* TKAlertCenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BD32971125B753900BE5EAF /* TKAlertCenter.m */; }; 3BD32973125B753900BE5EAF /* TKAlertCenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BD32971125B753900BE5EAF /* TKAlertCenter.m */; };
3BDE805E15BE3B210010738D /* NSArrayTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BDE805D15BE3B210010738D /* NSArrayTests.m */; }; 3BDE805E15BE3B210010738D /* NSArrayTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BDE805D15BE3B210010738D /* NSArrayTests.m */; };
3BE2036A149184CA003FFBEB /* UIViewController+TKCategory.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BE20368149184CA003FFBEB /* UIViewController+TKCategory.h */; };
3BE2036B149184CA003FFBEB /* UIViewController+TKCategory.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BE20369149184CA003FFBEB /* UIViewController+TKCategory.m */; };
3BE8FF8512945C4400DBF3FC /* UIScrollview+TKCategory.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BE8FF8312945C4400DBF3FC /* UIScrollview+TKCategory.h */; }; 3BE8FF8512945C4400DBF3FC /* UIScrollview+TKCategory.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BE8FF8312945C4400DBF3FC /* UIScrollview+TKCategory.h */; };
3BE8FF8612945C4400DBF3FC /* UIScrollview+TKCategory.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BE8FF8412945C4400DBF3FC /* UIScrollview+TKCategory.m */; }; 3BE8FF8612945C4400DBF3FC /* UIScrollview+TKCategory.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BE8FF8412945C4400DBF3FC /* UIScrollview+TKCategory.m */; };
3BEE1CBB12CFB78300FECF6D /* TKProgressCircleView.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BEE1CB912CFB78300FECF6D /* TKProgressCircleView.h */; }; 3BEE1CBB12CFB78300FECF6D /* TKProgressCircleView.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BEE1CB912CFB78300FECF6D /* TKProgressCircleView.h */; };
Expand Down Expand Up @@ -214,8 +212,6 @@
3BD32971125B753900BE5EAF /* TKAlertCenter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TKAlertCenter.m; sourceTree = "<group>"; }; 3BD32971125B753900BE5EAF /* TKAlertCenter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TKAlertCenter.m; sourceTree = "<group>"; };
3BDE805C15BE3B210010738D /* NSArrayTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSArrayTests.h; path = Tests/NSArrayTests.h; sourceTree = "<group>"; }; 3BDE805C15BE3B210010738D /* NSArrayTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSArrayTests.h; path = Tests/NSArrayTests.h; sourceTree = "<group>"; };
3BDE805D15BE3B210010738D /* NSArrayTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSArrayTests.m; path = Tests/NSArrayTests.m; sourceTree = "<group>"; }; 3BDE805D15BE3B210010738D /* NSArrayTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSArrayTests.m; path = Tests/NSArrayTests.m; sourceTree = "<group>"; };
3BE20368149184CA003FFBEB /* UIViewController+TKCategory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+TKCategory.h"; sourceTree = "<group>"; };
3BE20369149184CA003FFBEB /* UIViewController+TKCategory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+TKCategory.m"; sourceTree = "<group>"; };
3BE8FF8312945C4400DBF3FC /* UIScrollview+TKCategory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIScrollview+TKCategory.h"; sourceTree = "<group>"; }; 3BE8FF8312945C4400DBF3FC /* UIScrollview+TKCategory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIScrollview+TKCategory.h"; sourceTree = "<group>"; };
3BE8FF8412945C4400DBF3FC /* UIScrollview+TKCategory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIScrollview+TKCategory.m"; sourceTree = "<group>"; }; 3BE8FF8412945C4400DBF3FC /* UIScrollview+TKCategory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIScrollview+TKCategory.m"; sourceTree = "<group>"; };
3BEAE08E1096C61A00A0A372 /* TKWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TKWindow.h; sourceTree = "<group>"; }; 3BEAE08E1096C61A00A0A372 /* TKWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TKWindow.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -444,8 +440,6 @@
3BAC9A2010953E5200F235F1 /* UIImage+TKCategory.m */, 3BAC9A2010953E5200F235F1 /* UIImage+TKCategory.m */,
3BB96F5612B55CD600C38F46 /* UIImageView+TKCategory.h */, 3BB96F5612B55CD600C38F46 /* UIImageView+TKCategory.h */,
3BB96F5712B55CD600C38F46 /* UIImageView+TKCategory.m */, 3BB96F5712B55CD600C38F46 /* UIImageView+TKCategory.m */,
3BE20368149184CA003FFBEB /* UIViewController+TKCategory.h */,
3BE20369149184CA003FFBEB /* UIViewController+TKCategory.m */,
3B4346E212DA6A240076579B /* UIButton+TKCategory.h */, 3B4346E212DA6A240076579B /* UIButton+TKCategory.h */,
3B4346E312DA6A240076579B /* UIButton+TKCategory.m */, 3B4346E312DA6A240076579B /* UIButton+TKCategory.m */,
3B166A6D137F38D7006298E6 /* UIColor+TKCategory.h */, 3B166A6D137F38D7006298E6 /* UIColor+TKCategory.h */,
Expand Down Expand Up @@ -558,7 +552,6 @@
3B17D289142E3CD40090616F /* TKNetworkQueue.h in Headers */, 3B17D289142E3CD40090616F /* TKNetworkQueue.h in Headers */,
3B560B7214574E30005FBDBF /* TKImageCache.h in Headers */, 3B560B7214574E30005FBDBF /* TKImageCache.h in Headers */,
3B819B5414601DC500EC0097 /* TKViewController.h in Headers */, 3B819B5414601DC500EC0097 /* TKViewController.h in Headers */,
3BE2036A149184CA003FFBEB /* UIViewController+TKCategory.h in Headers */,
3BF9A833159791EA002A7F9E /* TKShapeView.h in Headers */, 3BF9A833159791EA002A7F9E /* TKShapeView.h in Headers */,
207DFF9215E1E3BE00A0BC2A /* NSDate+CalendarGrid.h in Headers */, 207DFF9215E1E3BE00A0BC2A /* NSDate+CalendarGrid.h in Headers */,
20DE9F4A168FA1EF00655BF8 /* NSObject+TKCategory.h in Headers */, 20DE9F4A168FA1EF00655BF8 /* NSObject+TKCategory.h in Headers */,
Expand Down Expand Up @@ -721,7 +714,6 @@
3B17D28A142E3CD40090616F /* TKNetworkQueue.m in Sources */, 3B17D28A142E3CD40090616F /* TKNetworkQueue.m in Sources */,
3B560B7314574E30005FBDBF /* TKImageCache.m in Sources */, 3B560B7314574E30005FBDBF /* TKImageCache.m in Sources */,
3B819B5514601DC500EC0097 /* TKViewController.m in Sources */, 3B819B5514601DC500EC0097 /* TKViewController.m in Sources */,
3BE2036B149184CA003FFBEB /* UIViewController+TKCategory.m in Sources */,
3BF9A834159791EA002A7F9E /* TKShapeView.m in Sources */, 3BF9A834159791EA002A7F9E /* TKShapeView.m in Sources */,
207DFF9315E1E3BE00A0BC2A /* NSDate+CalendarGrid.m in Sources */, 207DFF9315E1E3BE00A0BC2A /* NSDate+CalendarGrid.m in Sources */,
20DE9F4B168FA1EF00655BF8 /* NSObject+TKCategory.m in Sources */, 20DE9F4B168FA1EF00655BF8 /* NSObject+TKCategory.m in Sources */,
Expand Down
42 changes: 40 additions & 2 deletions src/TapkuLibrary/NSObject+TKCategory.h
Expand Up @@ -52,12 +52,50 @@
+ (id) createObject:(NSDictionary*)data; + (id) createObject:(NSDictionary*)data;






/** Imports data from an `NSDictionary` objects using the map provided by the dataKeys dictionary. /** Imports data from an `NSDictionary` objects using the map provided by the dataKeys dictionary.
@param dictionary The data that will be imported. @param dictionary The data that will be imported.
*/ */
- (void) importDataWithDictionary:(NSDictionary*)dictionary; - (void) importDataWithDictionary:(NSDictionary*)dictionary;




/** Process JSON data in the background with a callback selector.
@param data The JSON data.
@param callback The selector that will be performed upon the parsing of the json data. The process data will be included as an object with the selector.
*/
- (void) processJSONDataInBackground:(NSData *)data withCallbackSelector:(SEL)callback;

/** Process JSON data in the background with a callback selector.
@param data The JSON data.
@param callback The selector that will be performed upon the parsing of the json data. The process data will be included as an object with the selector.
@param options An json parsing options to be included will parsing the JSON data.
*/
- (void) processJSONDataInBackground:(NSData *)data withCallbackSelector:(SEL)callback readingOptions:(NSJSONReadingOptions)options;

/** Process JSON data in the background with a callback selector.
@param data The JSON data.
@param callback The selector that will be performed upon the parsing of the json data. The process data will be included as an object with the selector.
@param backgroundProcessor The selector that will be performed in the background upon the parsing of the json data. The process data will be included as an object with the selector. This selector must return some type of object to be passed to the callback selector.
@param options An json parsing options to be included will parsing the JSON data.
*/
- (void) processJSONDataInBackground:(NSData *)data withCallbackSelector:(SEL)callback backgroundSelector:(SEL)backgroundProcessor readingOptions:(NSJSONReadingOptions)options;

/** Process JSON data in the background with a callback selector.
@param data The JSON data.
@param callback The selector that will be performed upon the parsing of the json data. The process data will be included as an object with the selector.
@param backgroundProcessor The selector that will be performed in the background upon the parsing of the json data. The process data will be included as an object with the selector. This selector must return some type of object to be passed to the callback selector.
@param errroSelector The selector that will be called upon if there is an error parsing the data.
*/
- (void) processJSONDataInBackground:(NSData *)data withCallbackSelector:(SEL)callback backgroundSelector:(SEL)backgroundProcessor errorSelector:(SEL)errroSelector;

/** Process JSON data in the background with a callback selector.
@param data The JSON data.
@param callback The selector that will be performed upon the parsing of the json data. The process data will be included as an object with the selector.
@param backgroundProcessor The selector that will be performed in the background upon the parsing of the json data. The process data will be included as an object with the selector. This selector must return some type of object to be passed to the callback selector.
@param errroSelector The selector that will be called upon if there is an error parsing the data.
@param options An json parsing options to be included will parsing the JSON data.
*/
- (void) processJSONDataInBackground:(NSData *)data withCallbackSelector:(SEL)callback backgroundSelector:(SEL)backgroundProcessor errorSelector:(SEL)errroSelector readingOptions:(NSJSONReadingOptions)options;

@end @end
101 changes: 97 additions & 4 deletions src/TapkuLibrary/NSObject+TKCategory.m
Expand Up @@ -51,10 +51,6 @@ - (id) initWithDataDictionary:(NSDictionary*)dictionary{
[self importDataWithDictionary:dictionary]; [self importDataWithDictionary:dictionary];
return self; return self;
} }




- (void) importDataWithDictionary:(NSDictionary*)dictionary{ - (void) importDataWithDictionary:(NSDictionary*)dictionary{




Expand Down Expand Up @@ -88,4 +84,101 @@ - (void) importDataWithDictionary:(NSDictionary*)dictionary{


} }




#pragma mark - PROCESS JSON IN THE BACKGROUND

- (void) processJSONDataInBackground:(NSData *)data withCallbackSelector:(SEL)callback{

[self processJSONDataInBackground:data
withCallbackSelector:callback
backgroundSelector:nil
errorSelector:nil
readingOptions:0];

}

- (void) processJSONDataInBackground:(NSData *)data withCallbackSelector:(SEL)callback readingOptions:(NSJSONReadingOptions)options{

[self processJSONDataInBackground:data
withCallbackSelector:callback
backgroundSelector:nil
errorSelector:nil
readingOptions:options];

}

- (void) processJSONDataInBackground:(NSData *)data withCallbackSelector:(SEL)callback backgroundSelector:(SEL)backgroundProcessor readingOptions:(NSJSONReadingOptions)options{

[self processJSONDataInBackground:data
withCallbackSelector:callback
backgroundSelector:backgroundProcessor
errorSelector:nil
readingOptions:options];

}

- (void) processJSONDataInBackground:(NSData *)data withCallbackSelector:(SEL)callback backgroundSelector:(SEL)backgroundProcessor errorSelector:(SEL)errroSelector{

[self processJSONDataInBackground:data
withCallbackSelector:callback
backgroundSelector:backgroundProcessor
errorSelector:errroSelector
readingOptions:0];

}


- (void) processJSONDataInBackground:(NSData *)data
withCallbackSelector:(SEL)callback
backgroundSelector:(SEL)backgroundProcessor
errorSelector:(SEL)errroSelector
readingOptions:(NSJSONReadingOptions)options{


NSMutableDictionary *dict = [NSMutableDictionary dictionary];

[dict setObject:data forKey:@"data"];
[dict setObject:[NSNumber numberWithUnsignedInt:options] forKey:@"flags"];

if(callback) [dict setObject:NSStringFromSelector(callback) forKey:@"callback"];
if(backgroundProcessor) [dict setObject:NSStringFromSelector(backgroundProcessor) forKey:@"backgroundProcessor"];
if(errroSelector) [dict setObject:NSStringFromSelector(errroSelector) forKey:@"errroSelector"];


[self performSelectorInBackground:@selector(_processJSONData:) withObject:dict];


}


- (void) _processJSONData:(NSDictionary*)dict{
@autoreleasepool {
NSError *error = nil;

NSData *data = [dict objectForKey:@"data"];
NSUInteger options = [[dict objectForKey:@"flags"] unsignedIntValue];

NSString *callback = [dict objectForKey:@"callback"];
NSString *background = [dict objectForKey:@"backgroundProcessor"];
NSString *eSelector = [dict objectForKey:@"errroSelector"];

id object = [NSJSONSerialization JSONObjectWithData:data options:options error:&error];



if(error){
if(eSelector) [self performSelector:NSSelectorFromString(eSelector) withObject:error];
}else{
if(background) object = [self performSelector:NSSelectorFromString(background) withObject:object];
[self performSelectorOnMainThread:NSSelectorFromString(callback) withObject:object waitUntilDone:NO];
}


}
}




@end @end
1 change: 0 additions & 1 deletion src/TapkuLibrary/TapkuLibrary.h
Expand Up @@ -38,7 +38,6 @@
#import "UIImageView+TKCategory.h" #import "UIImageView+TKCategory.h"
#import "UIScrollview+TKCategory.h" #import "UIScrollview+TKCategory.h"
#import "UIButton+TKCategory.h" #import "UIButton+TKCategory.h"
#import "UIViewController+TKCategory.h"






Expand Down
77 changes: 0 additions & 77 deletions src/TapkuLibrary/UIViewController+TKCategory.h

This file was deleted.

0 comments on commit 61a4706

Please sign in to comment.