RealmSugar is a syntactic sugar to remove unnecessary code for retrieving notifications on realm instances.
RealmSugar is an extension of only 15 lines of code to make retrieving notifications more readable. You need less boilerplate code to get updates of each realm instance.
- Easy way to get realm object notifications with less boilerplate code.
- Looking for a way to improve it to avoid the need to guard type safety.
- Sugar to get notifications on Lists, Results and LinkingObjects.
- iOS 9.0+
- Xcode 8.0+
- Swift 3.0
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
CocoaPods 1.0.0+ is required to build RealmSugar.
To integrate RealmSugar into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target '<Your Target Name>' do
pod 'RealmSugar'
end
Then, run the following command:
$ pod install
Get notified on all changed properties.
let instance = Employee()
let token = instance.notify { [weak self] (employee) in
// Update label
self?.textLabel.text = employee.name
}
Get notified when an update comes in for specific properties.
let instance = Employee()
let token = instance.notify(for: #keyPath(Employee.name)) { [weak self] (employee) in
// Update label
self?.textLabel.text = employee.name
}
let token = instance.notify(for: [#keyPath(Employee.name)]) { [weak self] (employee) in
// Update label
self?.textLabel.text = employee.name
}
Get notified on initiation and on updates.
let instance = Employee()
let token = instance.fireAndNotify(for: #keyPath(Employee.name)) { [weak self] (employee) in
// Update label
self?.textLabel.text = employee.name
}
let token = instance.fireAndNotify(for: [#keyPath(Employee.name)]) { [weak self] (employee) in
// Update label
self?.textLabel.text = employee.name
}
Get notified on all changed objects.
let realm = try! Realm()
let token = realm.objects(Employee.self).notify { [weak self] (employees) in
// Log all employees
employees.forEach { dump($0) }
}
Specify for which kind of updates you want to get notified.
let realm = try! Realm()
let token = realm.objects(Employee.self).notify(when: .inserted) { [weak self] (employees) in
// Log all employees
employees.forEach { dump($0) }
}
let token = realm.objects(Employee.self).notify(when: [.inserted, .deleted]) { [weak self] (employees) in
// Log all employees
employees.forEach { dump($0) }
}
Get notified on initiation and on updates.
let realm = try! Realm()
let token = realm.objects(Employee.self).fireAndNotify(when: .inserted) { [weak self] (employees) in
// Log all employees
employees.forEach { dump($0) }
}
let token = realm.objects(Employee.self).fireAndNotify(when: [.inserted, .modified]) { [weak self] (employees) in
// Log all employees
employees.forEach { dump($0) }
}
Please make any pull requests to improve this, all help is welcome.
RealmSugar is released under the Apache license. See LICENSE for details.