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.
Why Might You Use This?
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 that doesn't lead to particularly readable or maintainable code. Most people just make their own domain objects subclass
PFObject, but that's also problematic for a number of reasons:
- You can't use other libraries/frameworks for model objects that depend on inheritance (such as Mantle's
- Depending on your app's structure, having your model objects be aware of how to persist to Parse may be a violation of the single responsibility principle. This isn't necessarily a problem, but it's less than ideal.
- The implementation of Parse's SDK is not open-source, making it more difficult to debug issues that might arise. Minimizing the presence of
PFObjectin your object graph can help reduce any pain this might cause.
- If you want to switch to using a different backend, be it a different BaaS or your own servers, removing Parse from your application will necessitate modifications to your client domain objects.
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
PFObjects and back as needed.
It is inspired by 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
PFObjects. If you have used
MTLParseAdapter will feel very familiar.
In order to serialize your objects to and from
PFObjects, 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 *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
MTLParseAdapter is available through CocoaPods. To install it, simply add the following line to your Podfile:
It includes Mantle and Parse as dependencies.
MTLParseAdapter is available under the MIT license. See the LICENSE file for more info.