CoreDataNotification 0.1.2

CoreDataNotification 0.1.2

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2016年12月
SwiftSwift 版本3.0
SPM支持 SPM

HocTran 维护。



  • 撰写者
  • HocTran

CoreDataNotification

关于 CoreDataNotification

这是一个轻量级库,用于处理 Core Data 通知。

它是如何工作的

监听任何来自 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("+++++++++++++++++++++++++")
        }
    }
}

要求

  • Swift 3.0

安装

CoreDataNotification

CoreDataNotification 可以通过 CocoaPods 获得。要安装它,只需将以下行添加到 Podfile 中

pod "CoreDataNotification"

RxSwift

支持 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 文件。