Arcade is a lightweight persistence layer for Swift structures or objects!
Models (structures) in your project must conform to Storable
.
import Arcade
struct Owner: Storable {
static var table: Table = AppTable.owner
var uuid: String
var name: String?
}
The Table
protocol defines the names of the tables in which to store your models.
import Arcade
enum AppTable: String, Table {
case owner = "Owner"
var name: String {
return self.rawValue
}
}
import Arcade
let arcade = Arcade(adapter: InMemoryAdapter())
import Arcade
arcade.connect().subscribe({ (success) in
// Connected!
}) { (error) in
// Error
}
import Arcade
let owner = Owner(uuid: UUID(), name: "Foo")
arcade.insert(storable: owner).subscribe({ (owner) in
// Inserted!
}) { (error) in
// Error
}
import Arcade
owner.name = "Fred"
arcade.update(storable: owner).subscribe({ (owner) in
// Updated!
}) { (error) in
// Error
}
import Arcade
let uuid = owner.uuid
arcade.delete(uuid: uuid, type: Owner.self).subscribe({ (success) in
// Deleted!
}) { (error) in
// Error
}
To find a specific item by UUID:
import Arcade
let future: Future<Owner> = arcade.find(uuid: owner.uuid)
future.subscribe({ (owner) in
guard let owner = owner else {
// Not found
}
// Found it!
}) { (error) in
// Error
}
To query a set of items:
import Arcade
let expression = Expression.equal("name", "Foo")
let query = Query.expression(expression)
let future: Future<Owner> = arcade.fetch(query: query)
future.subscribe({ (owners) in
// Do something with owners...
}) { (error) in
// Error
}
To use Arcade with the CoreData adapter some additional protocol conformance must be setup. Your
CoreData entites should conform to CoreDataStorable
:
@objc(OwnerEntity)
class OwnerEntity: NSManagedObject {
@NSManaged var uuid: UUID
@NSManaged var name: String?
override func awakeFromInsert() {
super.awakeFromInsert()
self.uuid = UUID()
}
}
extension OwnerEntity: CoreDataStorable {
public var viewable: Viewable {
return Owner(uuid: self.uuid, name: self.name)
}
public var storable: Storable {
return Owner(uuid: self.uuid, name: self.name)
}
public func update(withStorable dictionary: [String : Any]) -> Bool {
if let uuid = dictionary["uuid"] as? UUID {
self.uuid = uuid
}
if let name = dictionary["name"] as? String {
self.name = name
} else if dictionary["name"] is NSNull {
self.name = nil
}
return true
}
}
That's it! Now you can save your objects using the CoreDataAdapter
just like any other object!
Arcade is released under the MIT license. See LICENSE for details.