//
//  Store.swift
//  Habits
//
//  Created by Julian Grosshauser on 27/09/15.
//  Copyright © 2015 Julian Grosshauser. All rights reserved.
//

import RealmSwift

typealias Habits = Results<Habit>

class Store {
    
    //MARK: Properties

    private let realm: Realm

    var habits: Habits {
        return realm.objects(Habit)
    }

    //MARK: Initialization

    init(realm: Realm) {
        self.realm = realm
    }

    //MARK: Managing Habits

    func addHabit(habit: Habit) {
        realm.write { [unowned self] in
            self.realm.add(habit)
        }
    }

    func deleteHabit(habit: Habit) {
        realm.write { [unowned self] in
            self.realm.delete(habit)
        }
    }
}

//
//  Store.swift
//  Habits
//
//  Created by Julian Grosshauser on 27/09/15.
//  Copyright © 2015 Julian Grosshauser. All rights reserved.
//

import RealmSwift

typealias Habits = Results<Habit>

class Store {
    
    //MARK: Properties

    private let realm: Realm

    var habits: Habits {
        return realm.objects(Habit)
    }

    //MARK: Initialization

    init(realm: Realm) {
        self.realm = realm
    }

    //MARK: Managing Habits

    func addHabit(name: String) {
        let habit = Habit(name: name)

        realm.write { [unowned self] in
            self.realm.add(habit)
        }
    }

    func deleteHabit(habit: Habit) {
        realm.write { [unowned self] in
            self.realm.delete(habit)
        }
    }
}

Create `Habit` object in `addHabit(_:)`
Create `Habit` object in `addHabit(_:)`

Creating the `Habit` object inside the method makes it safe to call the method in a different thread (than the `Habit` object would have been created in before).
