Permalink
Browse files

Moving background JSON process methods to NSObject category from UIVi…

…ewController category.
  • Loading branch information...
1 parent 6e6a37e commit 61a4706d4cd303f101aef670a3a1f98ab5db1821 @devinross committed Feb 12, 2013
@@ -107,8 +107,6 @@
3BD32972125B753900BE5EAF /* TKAlertCenter.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BD32970125B753900BE5EAF /* TKAlertCenter.h */; };
3BD32973125B753900BE5EAF /* TKAlertCenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BD32971125B753900BE5EAF /* TKAlertCenter.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 */; };
3BE8FF8612945C4400DBF3FC /* UIScrollview+TKCategory.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BE8FF8412945C4400DBF3FC /* UIScrollview+TKCategory.m */; };
3BEE1CBB12CFB78300FECF6D /* TKProgressCircleView.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BEE1CB912CFB78300FECF6D /* TKProgressCircleView.h */; };
@@ -214,8 +212,6 @@
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>"; };
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>"; };
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>"; };
@@ -444,8 +440,6 @@
3BAC9A2010953E5200F235F1 /* UIImage+TKCategory.m */,
3BB96F5612B55CD600C38F46 /* UIImageView+TKCategory.h */,
3BB96F5712B55CD600C38F46 /* UIImageView+TKCategory.m */,
- 3BE20368149184CA003FFBEB /* UIViewController+TKCategory.h */,
- 3BE20369149184CA003FFBEB /* UIViewController+TKCategory.m */,
3B4346E212DA6A240076579B /* UIButton+TKCategory.h */,
3B4346E312DA6A240076579B /* UIButton+TKCategory.m */,
3B166A6D137F38D7006298E6 /* UIColor+TKCategory.h */,
@@ -558,7 +552,6 @@
3B17D289142E3CD40090616F /* TKNetworkQueue.h in Headers */,
3B560B7214574E30005FBDBF /* TKImageCache.h in Headers */,
3B819B5414601DC500EC0097 /* TKViewController.h in Headers */,
- 3BE2036A149184CA003FFBEB /* UIViewController+TKCategory.h in Headers */,
3BF9A833159791EA002A7F9E /* TKShapeView.h in Headers */,
207DFF9215E1E3BE00A0BC2A /* NSDate+CalendarGrid.h in Headers */,
20DE9F4A168FA1EF00655BF8 /* NSObject+TKCategory.h in Headers */,
@@ -721,7 +714,6 @@
3B17D28A142E3CD40090616F /* TKNetworkQueue.m in Sources */,
3B560B7314574E30005FBDBF /* TKImageCache.m in Sources */,
3B819B5514601DC500EC0097 /* TKViewController.m in Sources */,
- 3BE2036B149184CA003FFBEB /* UIViewController+TKCategory.m in Sources */,
3BF9A834159791EA002A7F9E /* TKShapeView.m in Sources */,
207DFF9315E1E3BE00A0BC2A /* NSDate+CalendarGrid.m in Sources */,
20DE9F4B168FA1EF00655BF8 /* NSObject+TKCategory.m in Sources */,
@@ -52,12 +52,50 @@
+ (id) createObject:(NSDictionary*)data;
-
-
/** Imports data from an `NSDictionary` objects using the map provided by the dataKeys dictionary.
@param dictionary The data that will be imported.
*/
- (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
@@ -51,10 +51,6 @@ - (id) initWithDataDictionary:(NSDictionary*)dictionary{
[self importDataWithDictionary:dictionary];
return self;
}
-
-
-
-
- (void) importDataWithDictionary:(NSDictionary*)dictionary{
@@ -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
@@ -38,7 +38,6 @@
#import "UIImageView+TKCategory.h"
#import "UIScrollview+TKCategory.h"
#import "UIButton+TKCategory.h"
-#import "UIViewController+TKCategory.h"
@@ -1,77 +0,0 @@
-//
-// UIViewController+TKCategory.h
-// Created by Devin Ross on 12/8/11.
-//
-/*
-
- tapku.com || http://github.com/devinross/tapkulibrary
-
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
-
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
-
- */
-
-#import <Foundation/Foundation.h>
-#import <UIKit/UIKit.h>
-
-/** Additional functionality for `UIViewController`. */
-@interface UIViewController (TKCategory)
-
-
-/** 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
Oops, something went wrong.

0 comments on commit 61a4706

Please sign in to comment.