Arcade is a lightweight persistence layer for Swift structures or objects!
Models (structures) in your project must conform to Storable.
import Arcade
structOwner:Storable{staticvartable:Table=AppTable.owner
varuuid:Stringvarname:String?}The Table protocol defines the names of the tables in which to store your models.
import Arcade
enumAppTable:String,Table{case owner ="Owner"varname:String{returnself.rawValue
}}import Arcade
letarcade=Arcade(adapter:InMemoryAdapter())import Arcade
arcade.connect().subscribe({(success)in
// Connected!
}){(error)in
// Error
}import Arcade
letowner=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
letuuid= 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
letfuture:Future<Owner>= arcade.find(uuid: owner.uuid)
future.subscribe({(owner)inguardlet owner = owner else{
// Not found
}
// Found it!
}){(error)in
// Error
}To query a set of items:
import Arcade
letexpression=Expression.equal("name","Foo")letquery=Query.expression(expression)letfuture: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)classOwnerEntity:NSManagedObject{@NSManagedvaruuid:UUID@NSManagedvarname:String?overridefunc awakeFromInsert(){
super.awakeFromInsert()self.uuid =UUID()}}extensionOwnerEntity:CoreDataStorable{publicvarviewable:Viewable{returnOwner(uuid:self.uuid, name:self.name)}publicvarstorable:Storable{returnOwner(uuid:self.uuid, name:self.name)}publicfunc update(withStorable dictionary:[String:Any])->Bool{iflet uuid =dictionary["uuid"]as?UUID{self.uuid = uuid
}iflet name =dictionary["name"]as?String{self.name = name
}elseifdictionary["name"] is NSNull{self.name =nil}returntrue}}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.