Skip to content

Instantly share code, notes, and snippets.

View kreeger's full-sized avatar

Ben Kreeger kreeger

View GitHub Profile

Keybase proof

I hereby claim:

  • I am kreeger on github.
  • I am kreeger (https://keybase.io/kreeger) on keybase.
  • I have a public key whose fingerprint is 65B5 0868 E045 FAEE E978 139E 876B 0A70 25B3 B62D

To claim this, I am signing this object:

@kreeger
kreeger / linux-bootstrap.sh
Last active January 26, 2019 17:20
Future machine setup. Run as root.
# Update apt and such
apt update
apt upgrade -y
apt dist-upgrade -y
# Add apt keys
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | apt-key add -
wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | apt-key add -
wget -q https://download.sublimetext.com/sublimehq-pub.gpg -O- | apt-key add -
@kreeger
kreeger / DataSourceUsage.swift
Created January 8, 2019 19:40
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))))")
@kreeger
kreeger / DeleteDatabase.swift
Created January 8, 2019 19:40
Example database deletion code for the blog post "Flapjack" on O'Reilly's engineering blog.
dataAccess.deleteDatabase(rebuild: true) { error in
if let error = error {
print(error.localizedDescription)
}
// It's almost as if it never happened.
}
@kreeger
kreeger / PerformInBackground.swift
Created January 8, 2019 19:39
Example background execution code for the blog post "Flapjack" on O'Reilly's engineering blog.
dataAccess.performInBackground { [weak self] context in
let pancake = context.create(Pancake.self, attributes: ["flavor": flavor, "radius": radius, "height": height])
let error = context.persist()
DispatchQueue.main.async {
guard let self = self else { return }
let foregroundPancake = self.dataAccess.mainContext.object(ofType: Pancake.self, objectID: pancake.objectID)
completion(foregroundPancake, error)
}
}
@kreeger
kreeger / FetchUsage.swift
Created January 8, 2019 19:39
Example fetch code for the blog post "Flapjack" on O'Reilly's engineering blog.
// Get every pancake.
let pancakes = dataAccess.mainContext.objects(ofType: Pancake.self)
// Get just the chocolate chip ones.
let pancakes = dataAccess.mainContext.objects(ofType: Pancake.self, attributes: ["flavor": "Chocolate Chip"])
// Create your own.
let pancake = dataAccess.mainContext.create(Pancake.self, attributes: ["flavor": "Rhubarb"])
// Save your changes.
let error = context.persist()
@kreeger
kreeger / Pancake.swift
Created January 8, 2019 19:39
Example model implementation code for the blog post "Flapjack" on O'Reilly's engineering blog.
extension Pancake: DataObject {
// The type of your primary key, if you have one of your own.
public typealias PrimaryKeyType = String
// The name of the entity as Core Data knows it.
public static var representedName: String {
return "Pancake"
}
// The key path to your model's primary key.
public static var primaryKeyPath: String {
return #keyPath(identifier)
@kreeger
kreeger / StackPrep.swift
Last active January 8, 2019 19:41
Stack preparation code for the blog post "Flapjack" on O'Reilly's engineering blog.
import Flapjack
// Create the DataAccess object, your main point-of-entry for persistence.
// You can also pass in `.sql(filename: "YourCoreDataStore.sql")`.
let dataAccess = CoreDataAccess(name: "YourCoreDataStore", type: .memory)
// Then tell the stack to configure itself.
dataAccess.prepareStack(asynchronously: true) { error in
if let error = error {
print(error.localizedDescription)
}
@kreeger
kreeger / .swiftlint.master.yml
Last active August 10, 2018 19:23
The SwiftList file for Flapjack + more. A work in progress.
# Reference:
# https://github.com/realm/SwiftLint/blob/master/Rules.md
disabled_rules:
# Disable rules that are in the default set
- colon # Disabled because it's too restrictive. See realm/SwiftLint#1901
- file_length # Maybe? What's the length for this?
- function_body_length # Maybe? What's the length for this?
- large_tuple # Maybe? What's the length for this, 2?
- line_length # Maybe? What's the length for this?
<UserSettings>
<ApplicationIdentity version="10.0"/>
<ToolsOptions>
<ToolsOptionsCategory name="Environment" RegisteredName="Environment"/>
</ToolsOptions>
<Category name="Environment_Group" RegisteredName="Environment_Group">
<Category name="Environment_FontsAndColors" Category="{1EDA5DD4-927A-43a7-810E-7FD247D0DA1D}" Package="{DA9FB551-C724-11d0-AE1F-00A0C90FFFC3}" RegisteredName="Environment_FontsAndColors" PackageName="Visual Studio Environment Package">
<PropertyValue name="Version">2</PropertyValue>
<FontsAndColors Version="2.0">
<Categories>