TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Dec 2016 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by HocTran.
It’s light weight library support to handle Core Data Notification.
Without CoreDataNotification
NotificationCenter.default.addObserver(token, selector: #selector(dataChanged(notification:)), name: NSNotification.Name.NSManagedObjectContextDidSave, object: self)
and then
func dataChanged(notification: NSNotification) {
//your code
}
With CoreDataNotification
let moc = DataController.default.managedObjectContext
moc.addNotificationBlock {
//deal with change here
}
Without CoreDataNotification
//Declare your fetch result
override func viewDidLoad() {
let moc = DataController.default.managedObjectContext
let fetchRequest = NSFetchRequest<City>(entityName: "City")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
SFetchedResultsController(fetchRequest: fetchRequestWithSort,
managedObjectContext: context,
sectionNameKeyPath: nil,
cacheName: nil)
fetchResultController?.delegate = self
}
//MARK: fetch controller delegate
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
}
With CoreDataNotification
override func viewDidLoad() {
super.viewDidLoad()
//notification
let moc = DataController.default.managedObjectContext
let fetchRequest = NSFetchRequest<City>(entityName: "City")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
notificationToken = moc.addNotificationBlock(fetchRequest: fetchRequest) { change in
switch change {
case .initial(let list):
self.cities = list
self.tableView.reloadData()
case .insert(let list, let insertion):
self.cities = list
self.tableView.insertRows(at: [insertion], with: .automatic)
case .delete(let list, let deletion):
self.cities = list
self.tableView.deleteRows(at: [deletion], with: .automatic)
case .update(let list, let modification):
self.cities = list
self.tableView.reloadRows(at: [modification], with: .automatic)
case .move(let list, let from, let to):
self.cities = list
self.tableView.moveRow(at: from, to: to)
case .error(let error):
print("++++++++ ERROR ++++++++")
print(error)
print("+++++++++++++++++++++++++")
}
}
}
CoreDataNotification is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "CoreDataNotification"
Available support for RxSwift. To install it, add the following line to your Podfiel:
pod "CoreDataNotification/RxSwift"
override func viewDidLoad() {
super.viewDidLoad()
let moc = DataController.default.managedObjectContext
moc.rx_notification()
.subscribe(
onNext: { _ in
//deal with changes in core data
})
.addDisposableTo(disposeBag)
let fetchRequest = NSFetchRequest<City>(entityName: "City")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
moc.rx_notification(fetchRequest: fetchRequest)
.catchError { error in
print("++++++++ ERROR ++++++++")
print(error)
print("+++++++++++++++++++++++++")
return Observable.just(CoreDataFetchResultChange<[City]>.initial([]))
}
.subscribe(
onNext: { change in
switch change {
case .initial(let list):
self.cities = list
self.tableView.reloadData()
case .insert(let list, let insertion):
self.cities = list
self.tableView.insertRows(at: [insertion], with: .automatic)
case .delete(let list, let deletion):
self.cities = list
self.tableView.deleteRows(at: [deletion], with: .automatic)
case .update(let list, let modification):
self.cities = list
self.tableView.reloadRows(at: [modification], with: .automatic)
case .move(let list, let from, let to):
self.cities = list
self.tableView.moveRow(at: from, to: to)
default:
break
}
}
onDisposed: { _ in
print("disposed")
}
)
.addDisposableTo(disposeBag)
}
To run the example project, clone the repo, and run pod install
from the Example directory first.
HocTran, [email protected]
CoreDataNotification is available under the MIT license. See the LICENSE file for more info.