RealmSugar 1.1.0

RealmSugar 1.1.0

TestsTested
LangLanguage SwiftSwift
License Apache 2
ReleasedLast Release Nov 2017
SwiftSwift Version 4.0
SPMSupports SPM

Maintained by Niels Koole.



  • By
  • Niels Koole

RealmSugar

RealmSugar is a syntactic sugar to remove unnecessary code for retrieving notifications on realm instances.

Summary

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.

Features

  • [x] Easy way to get realm object notifications with less boilerplate code.
  • [x] Looking for a way to improve it to avoid the need to guard type safety.
  • [x] Sugar to get notifications on Lists, Results and LinkingObjects.

Requirements

  • iOS 9.0+
  • Xcode 8.0+
  • Swift 3.0

Usage

Notifications on objects

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
}

Notifications on collections

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) }
}

Credits

Please make any pull requests to improve this, all help is welcome.

License

RealmSugar is released under the Apache license. See LICENSE for details.