Latest 1.0
Homepage https://github.com/Ovrchk/OVFetchKit
License MIT
Platforms ios 8.0
Authors

OVFetchKit provides tools for observing changes in fetch requests (using NSFetchedResultsController) and automatically updating you collection views such as UITableView or UICollectionView

Usage

You can create OVFetchObserver instance to observe updates in specific fetch…


class ViewController: UIViewController {

    var observer:OVFetchObserver<MyCoreDataEntity>!

    override func viewDidLoad() {
        super.viewDidLoad()

        let fetchRequest:NSFetchRequest! = nil //TODO: get fetch request somewhere
        let context:NSManagedObjectContext! = nil //TODO: get context somewhere

        self.observer = OVFetchObserver<MyCoreDataEntity>.init(fetchRequest: fetchRequest, managedObjectContext: context)
        self.observer.startObserving {(update) in

            //all fetch updates agregating in observer and can be handled within this closure

            print("Inserted: (update.insertedItems.count), updated: (update.updatedItems.count), deleted: (update.deletedItems.count), moved: (update.movedItems.count)")
    }
}

… or you can use OVCollectionDataSource class to create a data source for your collection view (UITableView/UICollectionView) and easily handle all data changes


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var tableView:UITableView!
    var dataSource:OVCollectionDataSource<MyCoreDataEntity>!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView = UITableView.init(frame: self.view.bounds, style: .plain)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.view.addSubview(self.tableView)

        let fetchRequest:NSFetchRequest! = nil //TODO: get fetch request somewhere
        let context:NSManagedObjectContext! = nil //TODO: get context somewhere

        self.dataSource = OVCollectionDataSource<MyCoreDataEntity>.init(fetchRequest: fetchRequest, managedObjectContext: context)
        self.dataSource.startObserving {[weak self] (update) in

            //you can relax, because all table modifications such as inserts, updates, etc. will be handled for you
            self?.tableView.applyUpdate(update)
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataSource.numberOfSections()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.numberOfObjectsInSection(section)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:UITableViewCell! = nil //TODO: dequeue cell

        //TODO: setup cell

        return cell
    }
}

You can also implement a lightweight cell updates (instead of reloading row) for better performance and specify row animation

let reloadRowBlock = { (cell, object, indexPath) in
    //update and layout your cell with given object without reloading row
}

self?.tableView.applyUpdate(update, reloadRowBlock: reloadRowBlock, rowAnimation: .fade)

Installation

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

pod 'OVFetchKit'

Author

Dmitry Overchuk, [email protected]

License

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

Latest podspec

{
    "name": "OVFetchKit",
    "version": "1.0",
    "summary": "A simple way to observe NSFetchRequest data changes and update UI",
    "description": "OVFetchKit provides tools for observing changes in fetch requests (using NSFetchedResultsController) and automatically updating you collection views such as UITableView or UICollectionView",
    "homepage": "https://github.com/Ovrchk/OVFetchKit",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Ovrchk": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Ovrchk/OVFetchKit.git",
        "tag": "1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "OVFetchKit/Classes/**/*"
}

Pin It on Pinterest

Share This