Share
Embed
Transcript
74 slides
-
@interface TGRBook : MTLModel @property (copy, nonatomic, readonly) NSString *author;
@property (copy, nonatomic, readonly) NSString *overview; @property (copy, nonatomic, readonly) NSArray *genres; @property (copy, nonatomic, readonly) NSDate *releaseDate; @property (copy, nonatomic, readonly) NSNumber *identifier; @property (copy, nonatomic, readonly) NSString *title; @property (copy, nonatomic, readonly) NSURL *coverURL; @end Example: Book Model -
@interface TGRBook : MTLModel @property (copy, nonatomic, readonly) NSString *author;
@property (copy, nonatomic, readonly) NSString *overview; @property (copy, nonatomic, readonly) NSArray *genres; @property (copy, nonatomic, readonly) NSDate *releaseDate; @property (copy, nonatomic, readonly) NSNumber *identifier; @property (copy, nonatomic, readonly) NSString *title; @property (copy, nonatomic, readonly) NSURL *coverURL; @end Example: Book Model Prefer immutable model objects -
NSError *error = nil; TGRBook *book = [TGRBook modelWithDictionary:@{ @"title"
: @"The Sandman", @"author" : @"Neil Gaiman", @"genres" : @[@"Graphic Novels", @"Fantasy"], ... } error:&error]; -
NSError *error = nil; TGRBook *book = [TGRBook modelWithDictionary:@{ @"title"
: @"The Sandman", @"author" : @"Neil Gaiman", @"genres" : @[@"Graphic Novels", @"Fantasy"], ... } error:&error]; TGRBook *anotherBook = [book copy]; -
NSError *error = nil; TGRBook *book = [TGRBook modelWithDictionary:@{ @"title"
: @"The Sandman", @"author" : @"Neil Gaiman", @"genres" : @[@"Graphic Novels", @"Fantasy"], ... } error:&error]; TGRBook *anotherBook = [book copy]; [NSKeyedArchiver archiveRootObject:book toFile:@"my_file"]; -
NSError *error = nil; TGRBook *book = [TGRBook modelWithDictionary:@{ @"title"
: @"The Sandman", @"author" : @"Neil Gaiman", @"genres" : @[@"Graphic Novels", @"Fantasy"], ... } error:&error]; TGRBook *anotherBook = [book copy]; [NSKeyedArchiver archiveRootObject:book toFile:@"my_file"]; TGRBook *b = [NSKeyedUnarchiver unarchiveObjectWithFile:@"my_file"]; -
(lldb) po book $0 = 0x0a349050 <TGRBook: 0xa349050> { author
= "Neil Gaiman, Sam Keith & Mike Dringenberg"; coverURL = "http://a4.mzstatic.com/us/r30/Publication/..."; genres = ( "Graphic Novels", Books, "Comics & Graphic Novels" ); identifier = 554016043; overview = "<p>NEW YORK TIMES bestselling author Neil Gaiman's..."; releaseDate = "2012-08-21 05:00:00 +0000"; title = "The Sandman, Vol. 1: Preludes & Nocturnes (New Edition)"; } -
Our Book Model could have a JSON representation { !
... ! "artistName": "Neil Gaiman, Sam Keith & Mike Dringenberg", ! "description": "<p>NEW YORK TIMES bestselling author...", ! "genres": ["Graphic Novels", "Books", "Comics & Graphic Novels"], ! "releaseDate": "2012-08-21T07:00:00Z", ! "trackId": 554016043, ! "trackName": "The Sandman, Vol. 1: Preludes & Nocturnes (New Edition)", ! "artworkUrl100": "http://a4.mzstatic.com/us/r30/Publication/...", ! ... } -
Our Book Model could have a JSON representation This is
how iTunes represents media (including books) { ! ... ! "artistName": "Neil Gaiman, Sam Keith & Mike Dringenberg", ! "description": "<p>NEW YORK TIMES bestselling author...", ! "genres": ["Graphic Novels", "Books", "Comics & Graphic Novels"], ! "releaseDate": "2012-08-21T07:00:00Z", ! "trackId": 554016043, ! "trackName": "The Sandman, Vol. 1: Preludes & Nocturnes (New Edition)", ! "artworkUrl100": "http://a4.mzstatic.com/us/r30/Publication/...", ! ... } -
Our Book Model could have a JSON representation This is
how iTunes represents media (including books) See http://www.apple.com/itunes/affiliates/resources/ documentation/itunes-store-web-service-search-api.html { ! ... ! "artistName": "Neil Gaiman, Sam Keith & Mike Dringenberg", ! "description": "<p>NEW YORK TIMES bestselling author...", ! "genres": ["Graphic Novels", "Books", "Comics & Graphic Novels"], ! "releaseDate": "2012-08-21T07:00:00Z", ! "trackId": 554016043, ! "trackName": "The Sandman, Vol. 1: Preludes & Nocturnes (New Edition)", ! "artworkUrl100": "http://a4.mzstatic.com/us/r30/Publication/...", ! ... } -
MTLJSONSerializing protocol Specify how to map properties to JSON keypaths
@interface TGRBook : MTLModel <MTLJSONSerializing> -
MTLJSONSerializing protocol Specify how to map properties to JSON keypaths
Specify how to convert a JSON value to a property key @interface TGRBook : MTLModel <MTLJSONSerializing> -
+ (NSDictionary *)JSONKeyPathsByPropertyKey { return @{ @"author" : @"artistName", @"overview"
: @"description", @"identifier" : @"trackId", @"title" : @"trackName", @"coverURL" : @"artworkUrl100" }; } -
Only properties with a corresponding ivar + (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{ @"author" : @"artistName", @"overview" : @"description", @"identifier" : @"trackId", @"title" : @"trackName", @"coverURL" : @"artworkUrl100" }; } -
Only properties with a corresponding ivar Property keys not present
in the dictionary are assumed to match the JSON + (NSDictionary *)JSONKeyPathsByPropertyKey { return @{ @"author" : @"artistName", @"overview" : @"description", @"identifier" : @"trackId", @"title" : @"trackName", @"coverURL" : @"artworkUrl100" }; } -
+ (NSValueTransformer *)releaseDateJSONTransformer { return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSString *str) { return
[self.dateFormatter dateFromString:str]; } reverseBlock:^(NSDate *date) { return [self.dateFormatter stringFromDate:date]; }]; } + (NSValueTransformer *)coverURLJSONTransformer { return [NSValueTransformer valueTransformerForName:MTLURLValueTransformerName]; } -
+<key>JSONTransformer methods + (NSValueTransformer *)releaseDateJSONTransformer { return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSString *str)
{ return [self.dateFormatter dateFromString:str]; } reverseBlock:^(NSDate *date) { return [self.dateFormatter stringFromDate:date]; }]; } + (NSValueTransformer *)coverURLJSONTransformer { return [NSValueTransformer valueTransformerForName:MTLURLValueTransformerName]; } -
+<key>JSONTransformer methods MTLValueTransformer is a block-based value transformer + (NSValueTransformer
*)releaseDateJSONTransformer { return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSString *str) { return [self.dateFormatter dateFromString:str]; } reverseBlock:^(NSDate *date) { return [self.dateFormatter stringFromDate:date]; }]; } + (NSValueTransformer *)coverURLJSONTransformer { return [NSValueTransformer valueTransformerForName:MTLURLValueTransformerName]; } -
+<key>JSONTransformer methods MTLValueTransformer is a block-based value transformer Predefined transformers:
MTLURLValueTransformerName and MTLBooleanValueTransformerName + (NSValueTransformer *)releaseDateJSONTransformer { return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSString *str) { return [self.dateFormatter dateFromString:str]; } reverseBlock:^(NSDate *date) { return [self.dateFormatter stringFromDate:date]; }]; } + (NSValueTransformer *)coverURLJSONTransformer { return [NSValueTransformer valueTransformerForName:MTLURLValueTransformerName]; } -
Mapping JSON child objects @interface TGRUser : MTLModel <MTLJSONSerializing> @property
(copy, nonatomic, readonly) NSArray *purchasedBooks; @property (copy, nonatomic, readonly) TGRBook *nowReading; @end -
Mapping JSON child objects @interface TGRUser : MTLModel <MTLJSONSerializing> @property
(copy, nonatomic, readonly) NSArray *purchasedBooks; @property (copy, nonatomic, readonly) TGRBook *nowReading; @end + (NSValueTransformer *)purchasedBooksJSONTransformer { -
Mapping JSON child objects @interface TGRUser : MTLModel <MTLJSONSerializing> @property
(copy, nonatomic, readonly) NSArray *purchasedBooks; @property (copy, nonatomic, readonly) TGRBook *nowReading; @end + (NSValueTransformer *)purchasedBooksJSONTransformer { return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; -
Mapping JSON child objects @interface TGRUser : MTLModel <MTLJSONSerializing> @property
(copy, nonatomic, readonly) NSArray *purchasedBooks; @property (copy, nonatomic, readonly) TGRBook *nowReading; @end + (NSValueTransformer *)purchasedBooksJSONTransformer { return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; } -
Mapping JSON child objects @interface TGRUser : MTLModel <MTLJSONSerializing> @property
(copy, nonatomic, readonly) NSArray *purchasedBooks; @property (copy, nonatomic, readonly) TGRBook *nowReading; @end + (NSValueTransformer *)purchasedBooksJSONTransformer { return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; } + (NSValueTransformer *)nowReadingJSONTransformer { -
Mapping JSON child objects @interface TGRUser : MTLModel <MTLJSONSerializing> @property
(copy, nonatomic, readonly) NSArray *purchasedBooks; @property (copy, nonatomic, readonly) TGRBook *nowReading; @end + (NSValueTransformer *)purchasedBooksJSONTransformer { return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; } + (NSValueTransformer *)nowReadingJSONTransformer { return [NSValueTransformer mtl_JSONDictionaryTransformerWithModelClass:TGRBook.class]; -
Mapping JSON child objects @interface TGRUser : MTLModel <MTLJSONSerializing> @property
(copy, nonatomic, readonly) NSArray *purchasedBooks; @property (copy, nonatomic, readonly) TGRBook *nowReading; @end + (NSValueTransformer *)purchasedBooksJSONTransformer { return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; } + (NSValueTransformer *)nowReadingJSONTransformer { return [NSValueTransformer mtl_JSONDictionaryTransformerWithModelClass:TGRBook.class]; } -
MTLJSONAdapter TGRBook *book = [MTLJSONAdapter modelOfClass:TGRBook.class fromJSONDictionary:JSONDictionary error:&error]; NSDictionary *dictionary
= [MTLJSONAdapter JSONDictionaryFromModel:book]; -
Do we still need Core Data? Of course! Memory efficiency
with large datasets -
Do we still need Core Data? Of course! Memory efficiency
with large datasets NSFetchRequest is cool! -
Do we still need Core Data? Of course! Memory efficiency
with large datasets NSFetchRequest is cool! Mantle and Core Data can work together -
MTLManagedObjectSerializing protocol Specify the entity name @interface TGRBook : MTLModel
<MTLManagedObjectSerializing> -
MTLManagedObjectSerializing protocol Specify the entity name Specify how to map
properties to managed object attributes @interface TGRBook : MTLModel <MTLManagedObjectSerializing> -
MTLManagedObjectSerializing protocol Specify the entity name Specify how to map
properties to managed object attributes Specify how to convert a managed object attribute value to a property key @interface TGRBook : MTLModel <MTLManagedObjectSerializing> -
+ (NSString *)managedObjectEntityName { return @"Book"; } + (NSDictionary *)managedObjectKeysByPropertyKey
{ return @{ @"coverURL" : @"coverLink" }; } + (NSValueTransformer *)coverURLEntityAttributeTransformer { return [[NSValueTransformer valueTransformerForName:MTLURLValueTransformerName] mtl_invertedTransformer]; } -
MTLManagedObjectAdapter NSManagedObject *managedBook = [MTLManagedObjectAdapter managedObjectFromModel:book insertingIntoContext:moc error:&error]; TGRBook *book
= [MTLManagedObjectAdapter modelOfClass:TGRBook.class fromManagedObject:object error:&error]; -
A delightful iOS and OS X networking framework Built on
top of Foundation technologies Easy to use block-based API Amazing community of developers Used by some of the most popular apps on iOS and OSX https://github.com/AFNetworking/AFNetworking -
Example: Search books NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com"]; AFHTTPClient *client
= [[AFHTTPClient alloc] initWithBaseURL:url]; [client getPath:@"search" parameters:@{ @"term" : @"the sandman", @"entity" : @"ebook" } success:^(AFHTTPRequestOperation *operation, id JSONResponse) { NSLog(@"Search results: %@", JSONResponse); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { ... }]; -
JSON Response { ! "resultCount": 50, ! "results": [{ !
! "artistId": 3603584, ! ! "artistName": "Neil Gaiman, Sam Kieth & Mike Dringenberg", ! ! "kind": "ebook", ! ! "price": 1.99, ! ! "description": "<p>The first issue of the first volume...", ! ! "currency": "USD", ! ! "genres": ["Graphic Novels", "Books", "Comics & Graphic Novels"], ! ! "genreIds": ["10015", "38", "9026"], ! ! "releaseDate": "2013-05-01T07:00:00Z", ! ! "trackId": 642469670, ! ! "trackName": "Sandman #1", ! ! ... -
Let’s add a Mantle [client getPath:@"search" parameters:@{ @"term" : @"Neil
Gaiman", @"entity" : @"ebook" } success:^(AFHTTPRequestOperation *operation, NSDictionary *JSONResponse) { NSArray *results = JSONResponse[@"results"]; NSValueTransformer *transformer; transformer = [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; NSArray *books = [transformer transformedValue:results]; NSLog(@"Books: %@", books); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { ... }]; -
Let’s add a Mantle [client getPath:@"search" parameters:@{ @"term" : @"Neil
Gaiman", @"entity" : @"ebook" } success:^(AFHTTPRequestOperation *operation, NSDictionary *JSONResponse) { NSArray *results = JSONResponse[@"results"]; NSValueTransformer *transformer; transformer = [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; NSArray *books = [transformer transformedValue:results]; NSLog(@"Books: %@", books); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { ... }]; NSArray *results = JSONResponse[@"results"]; NSValueTransformer *transformer; transformer = [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; NSArray *books = [transformer transformedValue:results]; -
NSArray *results = JSONResponse[@"results"]; NSValueTransformer *transformer; transformer = [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class];
NSArray *books = [transformer transformedValue:results]; -
Mapping can potentially take time NSArray *results = JSONResponse[@"results"]; NSValueTransformer
*transformer; transformer = [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; NSArray *books = [transformer transformedValue:results]; -
Mapping can potentially take time It should be done in
a background queue NSArray *results = JSONResponse[@"results"]; NSValueTransformer *transformer; transformer = [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; NSArray *books = [transformer transformedValue:results]; -
Mapping can potentially take time It should be done in
a background queue Boilerplate code again! NSArray *results = JSONResponse[@"results"]; NSValueTransformer *transformer; transformer = [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:TGRBook.class]; NSArray *books = [transformer transformedValue:results]; -
Overcoat The perfect accessory for Mantle Makes it dead simple
to use Mantle model objects with a RESTful client AFNetworking extension https://github.com/gonzalezreal/Overcoat -
OVCQuery *query = [OVCQuery queryWithMethod:OVCQueryMethodGet path:@"search" parameters:@{ @"term" : term,
@"entity" : @"ebook" } modelClass:TGRBook.class objectKeyPath:@"results"]; [client executeQuery:query completionBlock:^(OVCRequestOperation *operation, NSArray *books, NSError *error) { NSLog(@"Books: %@", books); }]; Overcoat 0.x -
Overcoat 1.0 NSDictionary *parameters = @{ @"term" : term, @"entity"
: @"ebook" }; [client GET:@"search" parameters:parameters resultClass:TGRBook.class resultKeyPath:@"results" completion:^(AFHTTPRequestOperation *operation, NSArray *books, NSError *error) { NSLog(@"Books: %@", books); }]; -
Overcoat 1.0 NSDictionary *parameters = @{ @"term" : term, @"entity"
: @"ebook" }; [client GET:@"search" parameters:parameters resultClass:TGRBook.class resultKeyPath:@"results" completion:^(AFHTTPRequestOperation *operation, NSArray *books, NSError *error) { NSLog(@"Books: %@", books); }]; Coming soon... -
Overcoat Server API requests are defined by OVCQuery objects. HTTP
method, path, parameters, model class, multipart data, etc. Maps JSON into model(s) In a private background queue