CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.

OrangeRealm 0.1.0

OrangeRealm 0.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Apr 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by pisces.



  • By
  • Steve Kim

  • OrangeRealm helps you safety multithreading and UI integration using Realm

Features

  • Thread safety
  • Simple interface
  • Easy UI integration
  • Support query limit, max, offset with filter, unlink
  • Abstraction for life cycle of Realm

Import

import OrangeRealm

Example

First - Create your RealmManager

Your RealmManager will match to one realm file by one to one

import RealmSwift
import OrangeRealm

class SampleRealmManager: AbstractRealmManager {

    // MARK: - Overridden: AbstractRealmManager

    override class var shared: AbstractRealmManager {
        struct Static {
            static let instance = SampleRealmManager()
        }
        return Static.instance
    }
    
    override var schemaVersion: UInt64 {
        return 1
    }
    
    override var fileURL: URL {
        return URL(fileURLWithPath: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!)/sample.realm", isDirectory: false)
    }
    
    override var objectTypes: [Object.Type]? {
        return [SampleObject.self]
    }
    
    override func deleteAll(_ realm: Realm) {
        realm.deleteAll()
    }
    
    override func process(forMigration migration: Migration, oldSchemaVersion: UInt64) {
    }
}

Second - Create your Realm Object

It is same to bagic implementation for realm object

import RealmSwift

class SampleObject: Object {
    dynamic var name: String?
    dynamic var id: Int = 0
    
    convenience init(id: Int, name: String?) {
        self.init()
        
        self.id = id
        self.name = name
    }
    
    override class func primaryKey() -> String? {
        return "id"
    }
}

And Last - Integrate your UI

This sample is integration with UITableView

result = SampleRealmManager.shared.query("id > 0", sortProperty: "id", ascending: false)
	// Sync with section of UITableView
    .set(section: 1)
    // Update UITableView after add notification for realm
    .changed({ [weak self] (section, deletions, insertions, modifications) in
        guard let weakSelf = self else {return}
        
        weakSelf.tableView.beginUpdates()
        weakSelf.tableView.deleteRows(at: deletions, with: .none)
        weakSelf.tableView.insertRows(at: insertions, with: .none)
        weakSelf.tableView.reloadRows(at: modifications, with: .none)
        weakSelf.tableView.endUpdates()
    })

self.tableView.reloadData()

Support results of 3 types - RealmQueryResult, Generic array, RealmResult

SampleRealmManager.shared.query()
SampleRealmManager.shared.objects()
SampleRealmManager.shared.results()

Doing limit, max query

let result = SampleRealmManager.shared.query("id > 0", sortProperty: "id", ascending: false, limit: 10, max: 10)

Doing offset query with filter

let offset = 2

let result = SampleRealmManager.shared.query("id > 0", sortProperty: "id", ascending: false) { (object) -> Bool in
    return object.id! > offset
}

Gain unlinked objects from Realm

let result = SampleRealmManager.shared.query("id > 0", sortProperty: "id", ascending: false, unlink: true)

Requirements

iOS Deployment Target 8.0 higher

Author

Steve Kim, [email protected]

License

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