Alex Barbulescu
Alex Barbulescu

Reputation: 331

How To Save Only One Instance Of Class In Realm

So instead of using user defualts I want to persist some settings using Realm.

I've created a class for the settings import Foundation import RealmSwift

class NutritionSettings: Object {
    @objc dynamic var calories: Int = 0
    @objc dynamic var proteins: Int = 0
    @objc dynamic var carbohydrates: Int = 0
    @objc dynamic var fats: Int = 0
}

But in my view controller I don't know how to save just one instance of it I've tried

let realm = try! Realm()
let settings = NutritionSettings()

    do {
    try realm.write{
        settings.calories = cals!
        settings.carbohydrates = carbs!
        settings.fats = fats!
        settings.proteins = proteins!
        }
    } catch {
        print("error saving settings")
    }

Since I know doing realm.add would just add another NutritionSettings object which is not what I want. I was unable to clarify anything using the documentation. Any help would be appreciated thanks.

Upvotes: 2

Views: 2514

Answers (2)

Nikita Tepliakov
Nikita Tepliakov

Reputation: 69

I faced a similar issue in my project when I tried to save a user session object. If you want to save a unique object, override the primaryKey() class method and set the unique key for it.

@objcMembers class NutritionSettings: Object {

     static let uniqueKey: String = "NutritionSettings"

     dynamic var uniqueKey: String = NutritionSettings.uniqueKey
     dynamic var calories: Int = 0

     override class func primaryKey() -> String? {
          return "uniqueKey"
     }
}

Then to receive the object just use the unique key.

// Saving

let settings = NutritionSettings()
settings.calories = 100

do {
    let realm = try Realm()
    try realm.write {
        realm.add(settings, update: .modified)
    }
} catch {
    // Error handling
}

// Reading

var settings: NutritionSettings?

do {
    let realm = try Realm()
    let key = NutritionSettings.uniqueKey
    settings = realm.object(ofType: NutritionSettings.self, forPrimaryKey: key)
} catch {
    // Error handling
}

if let settings = settings {
    // Do stuff
}

Hope it will help somebody.

Upvotes: 4

matt
matt

Reputation: 1

If you look at the example realm provides https://realm.io/docs/swift/latest you can see that in order to only save one object you still have to do an add. Once you have added the object to the database you can fetch that object and do a write that modifies the internal properties

let realm = try! Realm()
let settings = NutritionSettings()
settings.id = 1
do {
try realm.write{
     realm.add(settings)

    }
} catch {
    print("error saving settings")
}

Next you can fetch and modify that single instance that you saved

let realm = try! Realm()
let settings = realm.objects(NutritionSettings.self).filter("id == 1").first

do {
try realm.write{
    settings.calories = cals!
    settings.carbohydrates = carbs!
    settings.fats = fats!
    settings.proteins = proteins!
    }
} catch {
    print("error saving settings")
}

Upvotes: 0

Related Questions