-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'cnstoll_AF2.0Example' of github.com:mutualmobile/MMReco…
…rd into 1.3.0
- Loading branch information
Showing
8 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// AFServer.h | ||
// | ||
// Copyright (c) 2014 Mutual Mobile (http:https://www.mutualmobile.com/) | ||
// | ||
// 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 "MMServer.h" | ||
|
||
@class AFHTTPSessionManager; | ||
|
||
/** | ||
This server implementation is intended to be an example for how one might use AFNetworking 2.0 and | ||
its encapsulation of NSURLSession along with MMRecord. | ||
This is a basic implementation of `MMServer` that uses a simple configuration along with an | ||
AFHTTPSessionManager as the backbone for making requests. This class allows you to register | ||
your own AFHTTPSessionManager subclass to be used to make GET requests to whatever web server | ||
it is configured for. The AFHTTPSessionManager will be used to make the requests sent to this | ||
server. The response object is expected to be returned as an array or dictionary. Errors will get | ||
forwarded through the failureBlock if the request fails. | ||
This server implementation is not intended to be canonical. It is highly likely that this server | ||
will not be sufficient for complex use cases and APIs. In those cases, it is highly recommended | ||
that you subclass MMServer directly and provide your own implementation to fit your given API and | ||
server. This implementation may be used as a guide to do that. | ||
## AFNetworking | ||
AFNetworking is a highly popular iOS networking framework. AFNetworking 2.0 is required to use this | ||
server implementation. If you do not have AFNetworking 2.0 linked in your project, this server will | ||
not work. | ||
## Batching | ||
This server implementation does support batching. If the request batching system in MMRecord is | ||
used, then those requests will be batched using the registered AFHTTPSessionManager. | ||
## Cancellation | ||
This server implementation does support cancellation. In this case, the class name of the domain | ||
object is associated with the NSURLSessionDataTask. If the server is asked to cancel requests | ||
for a given domain, all NSURLSessionDataTasks associated with that domain will be cancelled. | ||
## Pagination | ||
This server implementation does NOT support pagination. You may, however, subclass this server | ||
class to provide your own page manager class, which will then support pagination. | ||
*/ | ||
|
||
|
||
@interface AFMMRecordServer : MMServer | ||
|
||
/** | ||
This method can be used to register an instance of an AFHTTPSessionManager. | ||
@param client The client this server should use to make requests. | ||
@return YES if the client was successfully registered. No otherwise. | ||
*/ | ||
+ (BOOL)registerAFHTTPSessionManager:(AFHTTPSessionManager *)sessionManager; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// AFServer.m | ||
// | ||
// Copyright (c) 2014 Mutual Mobile (http:https://www.mutualmobile.com/) | ||
// | ||
// 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 "AFMMRecordServer.h" | ||
|
||
#import <objc/runtime.h> | ||
#import "AFHTTPSessionManager.h" | ||
|
||
static id AFServer_registeredAFSessionManager; | ||
|
||
@implementation AFMMRecordServer | ||
|
||
+ (BOOL)registerAFHTTPSessionManager:(id)sessionManager { | ||
if ([sessionManager isKindOfClass:[AFHTTPSessionManager class]] || | ||
[[sessionManager class] isSubclassOfClass:[AFHTTPSessionManager class]]) { | ||
AFServer_registeredAFSessionManager = sessionManager; | ||
return YES; | ||
} | ||
|
||
return NO; | ||
} | ||
|
||
+ (void)cancelRequestsWithDomain:(id)domain { | ||
id sessionManager = AFServer_registeredAFSessionManager; | ||
|
||
if (domain) { | ||
NSArray *allTasks = [sessionManager dataTasks]; | ||
|
||
for (NSURLSessionDataTask *task in allTasks) { | ||
Class domainClass = [domain class]; | ||
|
||
if ([[self taskDomainFortask:task] isEqualToString:NSStringFromClass(domainClass)]) { | ||
[task cancel]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
+ (void)startRequestWithURN:(NSString *)URN | ||
data:(NSDictionary *)data | ||
paged:(BOOL)paged | ||
domain:(id)domain | ||
batched:(BOOL)batched | ||
dispatchGroup:(dispatch_group_t)dispatchGroup | ||
responseBlock:(void (^)(id responseObject))responseBlock | ||
failureBlock:(void (^)(NSError *error))failureBlock { | ||
NSString* newURN = [URN stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | ||
NSDictionary *newData = data; | ||
id sessionManager = AFServer_registeredAFSessionManager; | ||
|
||
if (paged) { | ||
|
||
} | ||
|
||
NSURLSessionDataTask *task = | ||
[sessionManager | ||
GET:newURN | ||
parameters:newData | ||
success:^(NSURLSessionDataTask *task, id responseObject) { | ||
if (responseBlock) { | ||
responseBlock(responseObject); | ||
} | ||
} | ||
failure:^(NSURLSessionDataTask *task, NSError *error) { | ||
if (failureBlock) { | ||
failureBlock(error); | ||
} | ||
}]; | ||
|
||
if (domain) { | ||
Class domainClass = [domain class]; | ||
|
||
[self setTaskDomain:NSStringFromClass(domainClass) onTask:task]; | ||
} | ||
} | ||
|
||
+ (id)taskDomainFortask:(id)task { | ||
return objc_getAssociatedObject(task, @"MMRecord_recordClassName"); | ||
} | ||
|
||
+ (void)setTaskDomain:(id)domain onTask:(id)task { | ||
objc_setAssociatedObject(task, @"MMRecord_recordClassName", domain, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
} | ||
|
||
@end |