测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2016年12月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 HocTran 维护。
这是一个轻量级库,用于处理 Core Data 通知。
没有 CoreDataNotification
NotificationCenter.default.addObserver(token, selector: #selector(dataChanged(notification:)), name: NSNotification.Name.NSManagedObjectContextDidSave, object: self)
然后是
func dataChanged(notification: NSNotification) {
//your code
}
有了 CoreDataNotification
let moc = DataController.default.managedObjectContext
moc.addNotificationBlock {
//deal with change here
}
没有 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?) {
}
有了 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 可以通过 CocoaPods 获得。要安装它,只需将以下行添加到 Podfile 中
pod "CoreDataNotification"
支持 RxSwift。要安装它,请将以下行添加到 Podfile 中
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)
}
要运行示例项目,首先克隆存储库,然后从 Example 文件夹中运行 pod install
HocTran,[email protected]
CoreDataNotification 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。