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

AFIncrementalStore

 AFIncrementalStore

* Audio of another version of this talk, given at Cocoaconf PDX 2012, is available here: http://soundcloud.com/mattt-thompson/syncing-with-afis

Second to views, networking is the hardest part of iOS programming. And it's several orders of magnitude more difficult if you plan to persist a local copy of the data in Core Data, and keep it in sync with the server. Nevermind the tricky theoretical issues of local cache invalidation and conflict resolution, it's hard enough to get Core Data set up in the first place! At least all of this was true before AFInrementalStore came around. Come learn how AFIncrementalStore works, and in doing so, get a deep dive into Core Data itself.

D29bb4d2d2f2ba2c2fb5a329e1e4651f?s=128

Mattt Thompson

October 11, 2012
Tweet

Transcript

  1. AFIncrementalStore Mattt Thompson Cocoaheads SF, October 2012

  2. @mattt

  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. None
  10. 
 … … … … AT&T

  11. None
  12. • Simple, Direct Interface

  13. • Simple, Direct Interface • Performant

  14. • Simple, Direct Interface • Performant • Lightweight

  15. • Simple, Direct Interface • Performant • Lightweight • Portable

  16. None
  17. Core Data

  18. Core Data • Object Consistency Across Contexts

  19. Core Data • Object Consistency Across Contexts • Faulting Behavior

  20. Core Data • Object Consistency Across Contexts • Faulting Behavior

    • Graphical Data Modeling
  21. Core Data • Object Consistency Across Contexts • Faulting Behavior

    • Graphical Data Modeling • Automatic Versioning & Migration
  22. Core Data • Object Consistency Across Contexts • Faulting Behavior

    • Graphical Data Modeling • Automatic Versioning & Migration • 
  23. None
  24. NSIncrementalStore

  25. Core Data HTTP Create Read Update Delete Insert POST Fetch

    / Refresh GET Update PUT / PATCH Delete DELETE
  26. AFIncrementalStore

  27. NSManagedObjectContext Reading NSFetchRequest • entity • predicate NSPersistentStoreCoordinator HTTPClient

  28. NSManagedObjectContext Reading NSFetchRequest • entity • predicate NSPersistentStoreCoordinator HTTPClient

  29. NSManagedObjectContext Reading NSFetchRequest • entity • predicate NSPersistentStoreCoordinator HTTPClient

  30. NSManagedObjectContext Reading NSFetchRequest • entity • predicate NSPersistentStoreCoordinator HTTPClient

  31. NSManagedObjectContext Reading NSFetchRequest • entity • predicate NSPersistentStoreCoordinator HTTPClient

  32. None
  33. NSManagedObjectContext

  34. NSPersistentStoreCoordinator NSManagedObjectContext

  35. NSPersistentStoreCoordinator NSManagedObjectContext AFIncrementalStore

  36. NSManagedObjectContext AFIncrementalStore NSPersistentStoreCoordinator

  37. NSManagedObjectContext AFIncrementalStore NSPersistentStoreCoordinator NSPersistentStoreCoordinator*

  38. NSManagedObjectContext AFIncrementalStore SQLite NSPersistentStoreCoordinator NSPersistentStoreCoordinator*

  39. Augmented Managed Object Model

  40. Z_PK Z_ENT Z_OPT ZPOSTID ZUSER Z_AF_RID 1 1 1 2

    1 1 3 1 2 914043 29 /posts/914043 914079 31 /posts/914079 915122 57 /posts/915122
  41. Z_PK Z_ENT Z_OPT ZPOSTID ZUSER Z_AF_RID 1 1 1 2

    1 1 3 1 2 914043 29 /posts/914043 914079 31 /posts/914079 915122 57 /posts/915122
  42. Automatic Fault Resolution

  43. NSManagedObjectContext NSSaveChangesRequest • inserted • updated • deleted NSPersistentStoreCoordinator HTTPClient

    Writing
  44. NSManagedObjectContext NSSaveChangesRequest • inserted • updated • deleted NSPersistentStoreCoordinator HTTPClient

    Writing
  45. NSManagedObjectContext NSSaveChangesRequest • inserted • updated • deleted NSPersistentStoreCoordinator HTTPClient

    Writing
  46. NSManagedObjectContext NSSaveChangesRequest • inserted • updated • deleted NSPersistentStoreCoordinator HTTPClient

    Writing
  47. Insert NSManagedObject

  48. Insert NSManagedObject <temporary id>

  49. Insert NSManagedObject <temporary id> HTTPClient

  50. Insert NSManagedObject <temporary id> HTTPClient POST

  51. Insert NSManagedObject <temporary id> HTTPClient POST

  52. Insert NSManagedObject <temporary id>

  53. Insert NSManagedObject <temporary id>

  54. - obtainPermanentIDsForObjects: error:

  55. Update NSManagedObject [permanent id]

  56. Update NSManagedObject [permanent id]

  57. Update NSManagedObject [permanent id] HTTPClient

  58. Update NSManagedObject [permanent id] HTTPClient PUT

  59. Update NSManagedObject [permanent id] HTTPClient PUT

  60. Update NSManagedObject [permanent id]

  61. Update NSManagedObject [permanent id]

  62. Delete NSManagedObject [permanent id]

  63. Delete NSManagedObject ☠

  64. NSManagedObject ☠ Delete HTTPClient

  65. NSManagedObject ☠ Delete HTTPClient DELETE

  66. Delete HTTPClient

  67. <AFIncrementalStoreHTTPClient>

  68. - representationsForRelationshipsFromRepresentation: ofEntity: fromResponse: - representationsFromResponseObject: {}

  69. /posts/123 - resourceIdentifierForRepresentation: ofEntity: fromResponse:

  70. Core Data → HTTP - requestWithFetchRequest: withContext: - requestWithMethod: pathForObjectWithID:

    withContext: - requestWithMethod: pathForRelationship: forObjectWithID: withContext: (Reading)
  71. Core Data → HTTP - representationOfAttributes: ofManagedObject: (Writing) - requestForInsertedObject:

    - requestForUpdatedObject: - requestForUpdatedObject:
  72. None
  73. • REST

  74. • REST • JSON-RPC / XML-RPC

  75. • REST • JSON-RPC / XML-RPC • SOAP

  76. • REST • JSON-RPC / XML-RPC • SOAP • OMGWTFBBQ

  77. AFRESTClient

  78. None
  79. GET /companies POST /suppliers PUT /customers/:id GET /country/:id/companies

  80. Rack::CoreData Generate REST API Scaffold from Core Data

  81. Philosophy

  82. I. The Server is the Canonical Source of Truth Philosophy

  83. I. The Server is the Canonical Source of Truth II.

    Be Chatty Philosophy
  84. I. The Server is the Canonical Source of Truth II.

    Be Chatty III. Use HTTP Caching Semantics Philosophy
  85. Features

  86. Features •Integrates with Any Core Data Stack

  87. Features •Integrates with Any Core Data Stack •Encapsulates All Networking

    Logic
  88. Features •Integrates with Any Core Data Stack •Encapsulates All Networking

    Logic •Automatically Syncs with Server
  89. Features •Integrates with Any Core Data Stack •Encapsulates All Networking

    Logic •Automatically Syncs with Server •Adapts to Any Web Service
  90. Features •Integrates with Any Core Data Stack •Encapsulates All Networking

    Logic •Automatically Syncs with Server •Adapts to Any Web Service •Reasonable Code Base (~600 LOC)
  91. The Future

  92. The Future •Documentation

  93. The Future •Documentation •Examples

  94. The Future •Documentation •Examples •Performance Optimization

  95. The Future •Documentation •Examples •Performance Optimization

  96. The Future •Documentation •Examples •Performance Optimization •Advanced Predicates

  97. The Future •Documentation •Examples •Performance Optimization •Advanced Predicates •REST Client

    Convenience Methods
  98. Should I Use AFIncremental Store?

  99. YES*

  100. github.com/ AFNetworking/ AFIncrementalStore

  101. Thanks!