DataLife

2.0.0

DataLife is an observable data storage class written in Swift
heroesofcode/DataLife

What's New

Improvements

2024-06-25T04:59:55Z

What's new?

  • New addObserver method for adding observers.
  • Support for UUID for unique observer identification.
  • Thread safety using DispatchQueue.

What's changed?

  • The observer method was renamed to addObserver for clarity.
  • Improvements to the notifyObservers method for code simplification.

Removed

  • Removal of redundant value != nil check.

CI SPM compatible GitHub

Overview

DataLife is an observable data storage class

Usage

  • In ViewModel inherit from DataLifeViewModel and create a variable that will be the result value.
import DataLife

final class ViewModel: DataLifeViewModel {
    
    var myName = DataLife<String>()
    
    func fetchMyName() {
        myName.value = "Darth Vader"
    }
    
}
  • In the ViewController you will call the variable that was created in the ViewModel that will be observed.
import UIKit

final class ViewController: UIViewController {
    
    @IBOutlet weak var nameLabel: UILabel!
    
    private let viewModel = ViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupState()
        setupFetchMyName()
    }

    private func setupFetchMyName() {
        viewModel.fetchMyName()
    }
    
    private func setupState() {
        viewModel.myName.addObserver(viewModel) { [weak self] name in
            self?.nameLabel.text = name
        }
    }
}

Demo

You see the demo here

Installation

import PackageDescription
let package = Package(
    name: "<Your Product Name>",
    dependencies: [
       .package(url: "https://github.com/heroesofcode/DataLife", .upToNextMajor(from: "2.0.0"))
    ],
    targets: [
        .target(
            name: "<Your Target Name>",
            dependencies: ["DataLife"]),
    ]
)

Contributing

To contribute, just fork this project and then open a pull request, feel free to contribute, bring ideas and raise any problem in the issue tab.

License

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

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Jul 02 2024 05:17:14 GMT-0900 (Hawaii-Aleutian Daylight Time)