Consider the following objects:
enum SetType: String {
case anaerobic, isometric
}
class RealmSet: Object {
@objc dynamic private var setType: String = ""
var type: SetType {
get {
guard let unwrappedSetType = SetType(rawValue: setType) else {
return .anaerobic
}
return unwrappedSetType
}
set {
self.setType = newValue.rawValue
}
}
}
protocol ExerciseSet {
var setType: SetType { get }
}
class Exercise: Object {
private let setsValues = List<RealmSet>()
var sets: [ExerciseSet] {
var sets = [ExerciseSet]()
for setValue in self.setsValues {
switch setValue.type {
case .anaerobic:
// Do Something
case .isometric:
// Do Something
}
}
return sets
}
}
What happens is that when I try to iterate this array of setsValues
and perform a switch
to discover which is the kind of that RealmSet
, instead type
getter property from RealmSet
gets called, the setter is called instead. As this object is a Realm object, the application instantly crashes because I'm not running a write transaction, which is true because I only need to access the property getter, not its setter.
The most strange of this situation is that this doesn't happen when Xcode versions is lower than 9.3. This only happens in Xcode 9.3 and 9.4. When I use Xcode 9.2 everything works perfectly.
I don't know where is the problem. If it is Realm lib, some change in Xcode versions 9.3 and 9.4 or what.
For those facing the same problem, do not use get set
when handling with Realm. Realm access properties through KVO, so in some situations it gets lost and cause this kind of error. Use get-only
properties combined with functions to set this properties instead using get set