CocoaPods trunk is moving to be read-only. Read more on the blog, there are 17 months to go.

MTLParseAdapter 0.1.0

MTLParseAdapter 0.1.0

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
 

  • By
  • Mike Walker

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 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:

  • You cannot gain functionality from subclassing a different class (such as Mantle's MTLModel).
  • This makes each of your model objects aware of how to persist to Parse, which depending on your application structure may be a violation of the single responsibility principle.
  • The implementation of Parse's SDK is not open-source, making it more difficult to debug issues that might arise. Minimizing the presence of PFObject in your object graph can help minimize any pain this might cause.
  • If you want to switch to using a different backend service, be it a different Backend-as-a-Service or your own servers, removing Parse from your application will involve changing 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 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 PFObjects. If you have used MTLJSONAdapter before, MTLParseAdapter will feel very familiar.

Usage

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

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.

Installation

It includes Mantle and Parse as dependencies.

Author

Mike Lazer-Walker

[email protected]

@lazerwalker

License

MTLParseAdapter is available under the MIT license. See the LICENSE file for more info.