Latest 0.1.2
Homepage https://github.com/HocTran/CoreDataNotification
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform

About CoreDataNotification

It’s light weight library support to handle Core Data Notification.

How it works

Listen any changes from Core Data.

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
}

Listen changes from Fetch result.

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("+++++++++++++++++++++++++")
        }
    }
}

Requirements

  • Swift 3.0

Installation

CoreDataNotification

CoreDataNotification is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "CoreDataNotification"

RxSwift

Available support for RxSwift. To install it, add the following line to your Podfiel:

pod "CoreDataNotification/RxSwift"

How it works

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

To run the example project, clone the repo, and run pod install from the Example directory first.

Author

HocTran, [email protected]

License

CoreDataNotification is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "CoreDataNotification",
    "version": "0.1.2",
    "summary": "Lightweight notification observer written in pure Swift for CoreData.",
    "description": "Lightweight notification observer for CoreData using Generics to provide more compile confidence, source elegent, less code.nnRxSwift extensions exist as well.nInstructions and documents are in [the README](https://github.com/HocTran/CoreDataNotification/)",
    "homepage": "https://github.com/HocTran/CoreDataNotification",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "HocTran": "[email protected]"
    },
    "source": {
        "git": "https://github.com/HocTran/CoreDataNotification.git",
        "tag": "0.1.2"
    },
    "platforms": {
        "ios": "8.0"
    },
    "default_subspecs": "Core",
    "pushed_with_swift_version": "3.0",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Source/Core/**/*"
        },
        {
            "name": "RxSwift",
            "source_files": "Source/RxSwift/**/*",
            "dependencies": {
                "CoreDataNotification/Core": [],
                "RxSwift": [
                    "~> 3.0"
                ]
            }
        }
    ]
}

Pin It on Pinterest

Share This