Skip to content

Instantly share code, notes, and snippets.

@kreeger
Created January 8, 2019 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kreeger/0e611302d5993dea6bb5b7635660b2d3 to your computer and use it in GitHub Desktop.
Save kreeger/0e611302d5993dea6bb5b7635660b2d3 to your computer and use it in GitHub Desktop.
Example data source usage code for the blog post "Flapjack" on O'Reilly's engineering blog.
let dataSourceFactory = CoreDataSourceFactory(dataAccess: dataAccess)
let queryAttributes = ["radius": 2.0, "flavor": "Chocolate Chip"]
let dataSource: CoreDataSource<Pancake> = dataSourceFactory.vendObjectsDataSource(attributes: queryAttributes, sectionProperty: "flavor", limit: 100)
// Prepare yourself for pancakes, but only chocolate chip ones bigger than a 2" radius, and no more than 100.
// This block fires every time the data source picks up an insert/change/deletion.
dataSource.onChange = { itemChanges, sectionChanges in
// If you've added `Flapjack/UIKit` to your Podfile, you get helper extensions!
self.tableView.performBatchUpdates(itemChanges, sectionChanges: sectionChanges)
// Get a specific pancake:
print("\(String(describing: dataSource.object(at: IndexPath(item: 0, section: 0))))")
}
// Kick off a call to start listening (and immediately fire `.onChange` with all existing results).
dataSource.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment