Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Parse for iOS

Parse for iOS

Isaac Schmidt's talk on Parse.com. More info, including sample code, at http://www.cocoaheadsnyc.org/2012/04/25/april-2012-isaac/.

CocoaHeadsNYC

March 08, 2012
Tweet

More Decks by CocoaHeadsNYC

Other Decks in Programming

Transcript

  1. Parse for iOS Rapid web-backing services

  2. TONIGHT ONLY • Introduction to Parse • Benefits & Drawbacks

    • Consuming web services in iOS • Coding with the Parse Framework • Demo! • Wrap-Up: Additional features Actually also available on the web any time
  3. Me • Isaac Schmidt @wryphonedev isaac@isaacschmidt.com • Indie dev, general

    tech enthusiast. • 3 years with iOS. Ask me about my apps!
  4. • Parse is a commercial platform for developing highly abstracted

    web services (e.g., simple cloud based persistence, or a new social network). • Parse provides SDKs for mobile devs, including a native Objective-C framework for iOS. • Allows rapid development of app-backing web services by obscuring many aspects otherwise required to develop and deploy web-service fueled apps. Overview of Parse
  5. What & How • If you’ve worked with CoreData and/or

    iCloud, you’ll find Parse very simple to introduce into your apps. • Actually simpler to implement a trivial persistence solution with Parse than either...
  6. Limitations • Dependency on Parse’s platform. • Parse is a

    black box. • Essentially no custom logic in your web services. • Schema-less data modeling (can be a +).
  7. iOS Data Landscape • CoreData • iCloud • File System

    • 3rd Party/Commercial Web Services • Custom Web Services
  8. Local Remote CoreData File System iCloud Sandboxed Access Controlled iOS

    App Data Model Parse Self-Rolled Web Service 3rd Party Content Providers
  9. General Benefits • Avoid overhead of deploying custom web services:

    No servers to configure or maintain, no time spent implementing support for users, queries, or parsing in server or client code. • Until you’re making 1,000,000 API requests a month, VC’s are paying for your EC2 hosting. • Not sandboxed
  10. Specific iOS Features • Provides network reachability and caches network-fetched

    content by default. • You essentially only interact with Objective-C model objects in your code.
  11. • Establish a network connection • Define and obtain a

    user reference • Authenticate the user to a web service • Query the web service • Parse response to produce results • Respond to results (eg, update UI or handle error condition) • Authenticate user if required • Query the web service • Respond to results (eg, update UI or handle error condition) Traditional w/ Parse Required Steps for Consuming Web Services
  12. Working with Parse • Create an app ID on parse.com

    • Add, link, and import the framework • Initialize Parse in your project with a call containing your app ID • Begin interacting with Parse objects
  13. Working with Parse • Most Parse API calls come in

    sync and async flavors • save / saveInBackground • getObject / getObjectInBackground • saveEventually (will save when network permits) • Completion handling with delegate callbacks or blocks • Today’s discussion is block-centric
  14. Parse Objects • PFUser & PFACL • PFObject • PFFile

    • PFQuery • PFPush • PFGeoPoint
  15. PFObject • PFObject • Attributes are assigned as key-value pairs

    • Values must be JSON-encodable • Strings, numbers, arrays, dictionaries • Consider a PFObject a model object (eg, NSManagedObject or NSObject subclass)
  16. PFObject PFObject *gameScore = [PFObject objectWithClassName:@"GameScore"]; [gameScore setObject:[NSNumber numberWithInt:1337] forKey:@"score"];

    [gameScore setObject:@"Joe" forKey:@"playerName"]; [gameScore setObject:[NSNumber numberWithBool:NO] forKey:@"cheatMode"]; [gameScore save]; •Model Class created lazily •createdAt & updatedAt attributed implicitly •Each object has unique ID •Relationships are defined by associations drawn in code (schema-less)
  17. PFUser • Secure and simple approach to “user” management •

    Hooks for linking Twitter/Facebook accounts • Anonymous/default users • [PFUser currentUser] singleton - (void)myMethod { PFUser *user = [PFUser user]; user.username = @"my name"; user.password = @"my pass"; user.email = @"email@example.com"; // other fields can be set just like with PFObject [user setObject:@"415-555-0101" forKey:@"phone"]; [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (!error) { // Hooray! Let them use the app now. } else { NSString *errorString = [[error userInfo] objectForKey:@"error"]; // Show the errorString somewhere and let the user try again. } }]; }
  18. Access Control w/ PFACL • Simple access-control scenario • You

    should almost always implement a default ACL, eg: [PFUser enableAutomaticUser]; PFACL *defaultACL = [PFACL ACL]; // Optionally enable public read access while disabling public write access. // [defaultACL setPublicReadAccess:YES]; [PFACL setDefaultACL:defaultACL withAccessForCurrentUser:YES]; • enableAutomaticUser
  19. PFQuery • Query/Fetch mechanism • getObjectWithID for specific object •

    findObjects for searches
  20. PFQuery Example PFQuery *query = [PFQuery queryWithClassName:@"GameScore"]; [query whereKey:@"playerName" equalTo:@"Isaac

    Schmidt"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { // The find succeeded. NSLog(@"Successfully retrieved %d scores.", objects.count); } else { // Log details of the failure NSLog(@"Error: %@ %@", error, [error userInfo]); } }]; Basic Find Query Compound Query PFQuery *lotsOfWins = [PFQuery queryWithClassName:@"Player"]; [lotsOfWins whereKey:@"wins" greaterThan:[NSNumber numberWithInt:150]]; PFQuery *fewWins = [PFQuery queryWithClassName:@"Player"]; [lotsOfWins whereKey:@"wins" lessThan:[NSNumber numberWithInt:5]]; PFQuery *query = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:fewWins,lotsOfWins,nil]]; [query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) { // results contains players with lots of wins or only a few wins. }];
  21. Additional PFQuery Features • Dictate caching policy • query.cachePolicy =

    kPFCachePolicyNetworkElseCache • Determine if a given object exists in cache • BOOL existsInCache = [query hasCachedResult] • Count objects matching query without fetching them • Use countObjects instead of findObjects
  22. Demo! • Bake a rich-media-web-backed-social- network in 10 minutes (or

    less!)
  23. Additional Features • Push Notifications • Initiate push notifications to

    groups or individuals in app or via web interface • User Interface • PFQueryTableViewController simplifies presenting lists-by-query • E-Mail Verification & Password Resets
  24. Reminders • Be careful what you gather and store •

    Users don’t like to discover that their address books have been uploaded. • If you must upload personal info, hash it first. • Maintain a transparent privacy policy. • Side note: Be careful what you NSLog! • Handle errors gracefully • It’s always exciting to move to the next feature. Resist the urge.
  25. Wrap Up • Great iOS API documentation, starter, and sample

    projects on parse.com • Responsive to questions • Find this presentation and sample code on cocoaheadsnyc.org