Skip to content

Instantly share code, notes, and snippets.

@andersio
Created April 26, 2017 05:03
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 andersio/179f35ff9ff60c8ebf81f8c6efbb19d1 to your computer and use it in GitHub Desktop.
Save andersio/179f35ff9ff60c8ebf81f8c6efbb19d1 to your computer and use it in GitHub Desktop.
// `Entity` protocol.
protocol Entity: Codeable {
var identifier: UInt { get }
}
protocol EntityIdentifer: Codeable {
var identifier: UInt { get }
}
struct Identifier<E: Entity>: EntityIdentifer {
var identifier: UInt { get }
}
struct Tweet: Entity {
var user: Identifier<User>
var content: String
var date: Date
}
// Query
let tweets: SignalProducer<[Tweet], NoError> = context.fetch(Tweet.self)
.filter(by: .date, condition: .greaterThan(Date().addingTimeInterval(-1000.0)))
.filter(by: .user, condition: .equal(Identifier<User>(1)))
// Query and Live Updating Instances
let tweets: SignalProducer<[Property<Tweet>], NoError> = context.properties(Tweet.self)
.filter(by: .date, condition: .greaterThan(Date().addingTimeInterval(-1000.0)))
.filter(by: .user, condition: .equal(Identifier<User>(1)))
// Live Updating Query
let tweets: SignalProducer<ArrayDelta<Tweet>, NoError> = context.collection(Tweet.self)
.filter(by: .date, condition: .greaterThan(Date().addingTimeInterval(-1000.0)))
.filter(by: .user, condition: .equal(Identifier<User>(1)))
// Live updating instance
let tweet: Property<Tweet> = context.property(for: Identifier<Tweet>(0))
// Live updating relationship
let tweet: Property<User> = context.property(for: Identifier<Tweet>(0))
.flatMap(.latest) { context.property(for: $0.user) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment