Skip to content

Commit

Permalink
Merge branch 'cnstoll_AF2.0Example' of github.com:mutualmobile/MMReco…
Browse files Browse the repository at this point in the history
…rd into 1.3.0
  • Loading branch information
Conrad Stoll committed Mar 1, 2014
2 parents c8a1c71 + d0a722e commit d14faa6
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
55D278BB18298377008CB6B4 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 55D278BA18298377008CB6B4 /* CoreLocation.framework */; };
55DB69CA184E66AA00677CEA /* Venues.json in Resources */ = {isa = PBXBuildFile; fileRef = 55DB69C9184E66AA00677CEA /* Venues.json */; };
55DB69CC184E66F400677CEA /* Venue.json in Resources */ = {isa = PBXBuildFile; fileRef = 55DB69CB184E66F400677CEA /* Venue.json */; };
55DD2B1D18C136DF00CF044B /* AFMMRecordServer.m in Sources */ = {isa = PBXBuildFile; fileRef = 55DD2B1C18C136DF00CF044B /* AFMMRecordServer.m */; };
55F8C4BB1848F8E000037D45 /* AFMMRecordResponseSerializer.m in Sources */ = {isa = PBXBuildFile; fileRef = 55F8C4BA1848F8E000037D45 /* AFMMRecordResponseSerializer.m */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -159,6 +160,8 @@
55D278BA18298377008CB6B4 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
55DB69C9184E66AA00677CEA /* Venues.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Venues.json; sourceTree = "<group>"; };
55DB69CB184E66F400677CEA /* Venue.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Venue.json; sourceTree = "<group>"; };
55DD2B1B18C136DF00CF044B /* AFMMRecordServer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFMMRecordServer.h; sourceTree = "<group>"; };
55DD2B1C18C136DF00CF044B /* AFMMRecordServer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFMMRecordServer.m; sourceTree = "<group>"; };
55F8C4B91848F8E000037D45 /* AFMMRecordResponseSerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFMMRecordResponseSerializer.h; sourceTree = "<group>"; };
55F8C4BA1848F8E000037D45 /* AFMMRecordResponseSerializer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFMMRecordResponseSerializer.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -280,6 +283,7 @@
55D278411829769B008CB6B4 /* Vendor */ = {
isa = PBXGroup;
children = (
55DD2B1A18C136DF00CF044B /* AFMMRecordServer */,
55D278421829769B008CB6B4 /* AFNetworking */,
55F8C4B81848F8E000037D45 /* AFMMRecordResponseSerializer */,
55D2789018297E01008CB6B4 /* MMRecord */,
Expand Down Expand Up @@ -413,6 +417,16 @@
name = Resources;
sourceTree = "<group>";
};
55DD2B1A18C136DF00CF044B /* AFMMRecordServer */ = {
isa = PBXGroup;
children = (
55DD2B1B18C136DF00CF044B /* AFMMRecordServer.h */,
55DD2B1C18C136DF00CF044B /* AFMMRecordServer.m */,
);
name = AFMMRecordServer;
path = ../../../../../Source/AFMMRecordServer;
sourceTree = "<group>";
};
55F8C4B81848F8E000037D45 /* AFMMRecordResponseSerializer */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -536,6 +550,7 @@
55D2785D1829769B008CB6B4 /* AFURLResponseSerialization.m in Sources */,
55D2785B1829769B008CB6B4 /* AFURLConnectionOperation.m in Sources */,
55D27866182977F5008CB6B4 /* MMFoursquareSessionManager.m in Sources */,
55DD2B1D18C136DF00CF044B /* AFMMRecordServer.m in Sources */,
55D2785A1829769B008CB6B4 /* AFSecurityPolicy.m in Sources */,
55D278AD18297E01008CB6B4 /* MMServerPageManager.m in Sources */,
55D278581829769B008CB6B4 /* AFHTTPSessionManager.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@interface MMFoursquareSessionManager : AFHTTPSessionManager

+ (instancetype)sharedClient;
+ (instancetype)serverClient;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ + (instancetype)sharedClient {
return _sharedClient;
}

+ (instancetype)serverClient {
static MMFoursquareSessionManager *_serverClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_serverClient = [[MMFoursquareSessionManager alloc] initWithBaseURL:[NSURL URLWithString:MMFoursquareAPIBaseURLString]];
[_serverClient setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]];
});

return _serverClient;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@

#import "AFMMRecordResponseSerializer.h"
#import "AFMMRecordResponseSerializationMapper.h"
#import "AFMMRecordServer.h"
#import "FSRecord.h"
#import "MMDataManager.h"
#import "MMFoursquareSessionManager.h"

@implementation MMAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MMFoursquareSessionManager *sessionManager = [MMFoursquareSessionManager sharedClient];
MMFoursquareSessionManager *serverClientManager = [MMFoursquareSessionManager serverClient];

[AFMMRecordServer registerAFHTTPSessionManager:serverClientManager];
[FSRecord registerServerClass:[AFMMRecordServer class]];

MMFoursquareSessionManager *sessionManager = [MMFoursquareSessionManager sharedClient];

NSManagedObjectContext *context = [[MMDataManager sharedDataManager] managedObjectContext];
AFHTTPResponseSerializer *HTTPResponseSerializer = [AFJSONResponseSerializer serializer];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import "Location.h"

#import "MMVenueViewController.h"
#import "MMDataManager.h"

@interface MMViewController ()

Expand All @@ -26,6 +27,12 @@ @implementation MMViewController
- (void)viewDidLoad {
[super viewDidLoad];

self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self
action:@selector(refreshVenues)
forControlEvents:UIControlEventValueChanged];
[self setRefreshControl:self.refreshControl];

NSString *oAuthToken = @"RMRLHPHOTZBIHKAX2G1XMZ33XQYDYKVCAUTM5GCTAA03X04F";

[[MMFoursquareSessionManager sharedClient]
Expand All @@ -42,6 +49,30 @@ - (void)viewDidLoad {
}];
}

- (void)refreshVenues {
NSManagedObjectContext *context = [[MMDataManager sharedDataManager] managedObjectContext];

NSString *oAuthToken = @"RMRLHPHOTZBIHKAX2G1XMZ33XQYDYKVCAUTM5GCTAA03X04F";

[Venue
startRequestWithURN:@"venues/search?ll=30.25,-97.75"
data:@{@"oauth_token": oAuthToken, @"v": @"20131105"}
context:context
domain:nil
resultBlock:^(NSArray *records) {
NSArray *venues = records;

self.venues = venues;

[self.tableView reloadData];

[self.refreshControl endRefreshing];
}
failureBlock:^(NSError *error) {
[self.refreshControl endRefreshing];
}];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
Expand Down
10 changes: 10 additions & 0 deletions MMRecord.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions Source/AFMMRecordServer/AFMMRecordServer.h
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
104 changes: 104 additions & 0 deletions Source/AFMMRecordServer/AFMMRecordServer.m
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

0 comments on commit d14faa6

Please sign in to comment.