CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.

ObservableKit 1.0.4

ObservableKit 1.0.4

Maintained by John Lima.



ObservableKit

GitHub release Build Status CocoaPods GitHub repo size License Platform Donate

ObservableKit is the easiest way to observe values in Swift.

❗️Requirements

  • iOS 9.3+
  • Swift 5.1+

Installation

Swift Package Manager

ObservableKit is available through SPM. To install it, follow the steps:

Open Xcode project > File > Swift Packages > Add Package Dependecy

After that, put the url in the field: https://github.com/thejohnlima/ObservableKit.git

CocoaPods

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

pod 'ObservableKit'

and run pod install

🎓 How to use

Import library in your swift file:

import ObservableKit

Download image example
Setup ObservableKit in your ViewModel:

let observable: OKObservable<OKState<Data, Error>> = OKObservable(OKState.loading)

func fetchImage() {
  let url = URL(string: model.image)!
  observable.value = .loading
  URLSession.shared.dataTask(with: url, completionHandler: { data, _, error in
    if let error = error {
      self.observable.value = .errored(error: error)
      return
    }
    self.observable.value = data != nil ? .load(data: data!) : .empty
  }).resume()
}

Than, in your view controller call the observer:

@IBOutlet weak var imageView: UIImageView!

private let viewModel = ViewModel()

override func viewDidLoad() {
  super.viewDidLoad()
  addObservers()
}

private func addObservers() {
  viewModel.observable.didChange = { [weak self] status in
    DispatchQueue.main.async {
      switch status {
      case .load(data: let data):
        print("✅ fetch image with succss")
        let image = UIImage(data: data)
        self?.imageView.image = image
      case .loading:
        print("🚀 loading data...")
      case .errored(error: let error):
        print("❌ get an error: \(error)")
      case .empty:
        print("❌ data not found")
      }
    }
  }
}

private func loadImage() {
  viewModel.fetchImage()
}

🙋🏻‍ Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request. 👨🏻‍💻

📜 License

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