Giter Club home page Giter Club logo

owlrealmcollectionview's Introduction

OwlRealmCollectionView

This is a version for Realm. If you are using CoreData, take a look into this version: https://github.com/thanhtanh/OwlCollectionView

Are you tired when you want to make data be synced between viewcontrollers? Such as you want:

  • When you add an item, it will be added into list immediately
  • When you delete/edit an item from Realm, it will be deleted/edited in list
  • When you change the value of one item, and want to reorder the items in list, it also do that for you, immediately

This library is written to listen the data changed event from Realm, then update data to UI for you. You will not have to implement Realm notifications by youself for every viewcontroller.

How to use

Configuration:

What you need is two classes OwlRealmCVDataSource and OwlRealmCVDelegate. So, copy them to anywhere in your project. Because this library use Realm, so you must have RealmSwift in your Podfile.

Implementation

When you want to implement a UICollectionView in your project, you usually do steps like below:

  • Add a UICollectionView to your storyboard. Assume that I name it collectionView
  • Add cells to your collection view.
  • Prepare datasource for you collection view
  • Make your viewcontroller conform UICollectionViewDelegate, UICollectionViewDataSource

With OwlRealmCollectionView, you just need to change some small things:

  • Create collection view data source with OwlRealmCVDataSource

      let dataSource = OwlRealmCVDataSource.datasourceConfig(withClass: Owl.self, predicate: NSPredicate.init(value: true), sortBy: "height", sortAscending: true)
      // `Owl` is a Realm object, 'height' is one property of Owl 
    
  • Create a subclass of OwlRealmCVDelegate and implement any method of UICollectionViewDelegate and UICollectionViewDataSource as the normal way

      class OwlDel: OwlRealmCVDelegate {
          weak var vc: ViewController!
    
          class func delegateForCV(grid:UICollectionView,
              withVC vc:ViewController,
              datasource:OwlRealmCVDataSource) -> OwlDel {
              let delegate = OwlDel()
    
              delegate.vc = vc
              delegate.collectionView = grid
    
              grid.delegate = delegate
              grid.dataSource = delegate
    
              delegate.dataSource = datasource
    
              return delegate
          }
    
          override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
              let obj = self.dataObject(at: indexPath) as! Owl
    
              ....
              return item
          }
    
          override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
              return CGSize(width: 200, height: 50)
          }
      }
    
  • Initial an delegate object for your collection view

      var del: OwlDel! // class variable
      // init with the dataSource we create above
      // `grid` is the collectionview we want to use
      self.del = OwlDel.delegateForCV(grid: self.grid, withVC:self, datasource:dataSource)
    
  • Reload collectionview

      self.grid.reloadData()
    

Example

Please take a look on my attached example to more clean what I said above. Screenshot:

screenshot

Explain the screenshot:

  • Requirement: A timer will add an owl after each 2 seconds with random height (the number at the end of each item). If the new owl with the height already existed in the list, it just been updated info (a random color) only. If height = 2, remove that existed owl. All owls in the list will be sorted by height.
  • Solve: At you can see the example in project, I just add/update/delete new owl to Realm, and don't care how UI update. It also update when you a view controller is not displayed to user.

License

OwlRealmCollectionView is released under the MIT license. See LICENSE for details.

owlrealmcollectionview's People

Contributors

thanhtanh avatar

Stargazers

Andy Cheng avatar Son Huynh avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.