CocoaPods trunk is moving to be read-only. Read more on the blog, there are 17 months to go.
TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Feb 2015 |
Maintained by Mike Lazer-Walker.
Depends on: | |
Asterism | ~> 1.0 |
Mantle | ~> 1.5 |
Parse | ~> 1.6 |
MTLParseAdapter is a small class that lets you easily serialize and deserialize your app's model objects to and from Parse's PFObject
class using Mantle.
Parse's iOS library encourages you to model your data in one of two ways. You can use PFObject
as a key-value store, similar to how one would use NSDictionary
, but doing this you lose a lot of the convenience and type safety of properties. Alternatively, you can make your own domain objects subclass PFObject
, but this is problematic for a number of reasons:
MTLModel
).PFObject
in your object graph can help minimize any pain this might cause.MTLParseAdapter
gives you a way to avoid these problems. It allows you to model your domain objects as MTLModel
Mantle objects, and serialize them into PFObject
s and back as needed.
It is modelled after and built on top of the JSON serialization functionality in Mantle. If your domain objects are already MTLModel
subclasses that conform to MTLJSONSerializing
, no additional work should be needed to allow MTLParseAdapter to convert your objects to and from PFObject
s. If you have used MTLJSONAdapter
before, MTLParseAdapter
will feel very familiar.
In order to serialize your objects to and from PFObject
s, they will need to (a) subclass MTLModel
and (b) implement the MTLJSONSerializing
protocol. For more information on this, check out the Mantle documentation.
After doing this, you can use MTLParseAdapter
to convert your model objects.
User *user = [[User alloc] init];
PFObject *parseObject = [MTLParseAdapter parseObjectFromModel:user error:&error];
PFObject *parseObject; // Fetched via a PFQuery, for instance
User *user = [MTLParseAdapter modelOfClass:User.class
fromParseObject:parseObject
error:&error];
If your Parse class names are identical to the names of your Objective-C object classes, you can omit the model class when deserializing from PFObject
PFObject *parseObject = [PFObject objectWithClassName:@"User"];
User *user = [MTLParseAdapter modelFromParseObject:parseObject error:&error];
Equivalent methods exist to convert arrays of multiple objects of the same class.
User *user1 = [[User alloc] init];
User *user2 = [[User alloc] init];
NSArray *users = @[user1, user2];
PFObject *parseObject = [MTLParseAdapter parseObjectsFromModels:users error:&error];
PFObject *parseObject1 = [PFObject objectWithClassName:@"User"];
PFObject *parseObject2 = [PFObject objectWithClassName:@"User"];
NSArray *parseObjects = @[parseObject1, parseObject2];
User *user = [MTLParseAdapter modelsOfClass:User.class
fromParseObjects:parseObjects
error:&error];
As above, there is also a modelsFromParseObjects:
method that infers the Objective-C class of each PFObject
based on its parseClassName
.
It includes Mantle and Parse as dependencies.
Mike Lazer-Walker
@lazerwalker
MTLParseAdapter is available under the MIT license. See the LICENSE file for more info.