Skip to content

Support Realm CRUD functions. Support Dao Object and Protocol Extension.

License

Notifications You must be signed in to change notification settings

yokurin/RealmSwiftService

Repository files navigation

RealmSwiftService

Support default CRUD functions.

Platform License

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Open Example/RealmSwiftService.xcworkspace and run Example to see a simple demonstration.

Functions

all() -> Results<ObjectType>
find(by key: Any) -> ObjectType?
first() -> ObjectType?
last() -> ObjectType?
deleteAll() throws -> Bool
delete(list: List<ObjectType>) throws -> Bool
save(update: Bool) throws -> Bool
delete() throws -> Bool

Usages

Support 2 way implementation.

1. Implements RealmAccessible Protocol ( Protocol Extension )

@objcMembers
class Cat: Object, RealmAccessible {

    dynamic var id = UUID().uuidString
    dynamic var name = ""

    override static func primaryKey() -> String? { return "id" }

    convenience init(name: String) {
        self.init()
        self.name = name
    }
}

// Save Realm Object
let cat = Cat(name: "tama")
try! cat.save()

// Get Realm Objects
Cat.all().forEach { print("\($0.name)-\($0.id)") } // tama-UUIDString

// RealmAccessible Example
// Instance Methods
let cat = Cat(name: "tama")  // Create Realm Object
try! cat.save()              // Save Object of Cat
try! cat.delete()            // Delete Object of Cat
// Static Methods
_ = Cat.all()                // Get All Object of Cat
_ = Cat.find(by: "id")       // Find Cat by id property
_ = Cat.first()              // Get First Object of Cat
_ = Cat.last()               // Get Last Object of Cat
try! Cat.deleteAll()         // Delete All Object of Cat

2. Use RealmDao

@objcMembers
class Dog: Object {

    dynamic var id = UUID().uuidString
    dynamic var name = ""

    override static func primaryKey() -> String? { return "id" }

    convenience init(name: String) {
        self.init()
        self.name = name
    }
}

// Save Realm Object
let dao = RealmDao<Dog>()
let dog = Dog(name: "pochi")
dao.save(dog)

// Get Realm Objects
dao.all().forEach { print("\($0.name)-\($0.id)") } // pochi-UUIDString

// Dao Example
let dao = RealmDao<Dog>()    // Create Dao of Dog
let dog = Dog(name: "pochi") // Create Realm Object
_ = dao.all()                // Get All Object of Dog
_ = dao.find(by: "id")       // Find Dog by id property
_ = dao.first()              // Get First Object of Dog
_ = dao.last()               // Get Last Object of Dog
try! dao.save(dog)           // Save Object of Dog
try! dao.delete(dog)         // Delete Object of Dog
try! dao.deleteAll()         // Delete All Object of Dog

Requirements

  • iOS 10.0+
  • Xcode 10.0+
  • Swift 4.2+

Installation

Manual

Add RealmAccessible.swift, RealmDao.swift into your Project.

CocoaPods

WIP...

RealmSwiftService is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RealmSwiftService'

and run pod install

Author

Tsubasa Hayashi, [email protected]

License

RealmSwiftService is available under the MIT license. See the LICENSE file for more info.

About

Support Realm CRUD functions. Support Dao Object and Protocol Extension.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published