How to make abstract objects in Realm.io in Swift
I am moving from CoreData to Realm.io. I did a little experiment to see how Realm.io handles situations where I need to subclass a class that is an RLMObject.
Model
import Realm
@objc enum RecurrenceEnum : Int {
case Daily = 1
case Weekly = 2
case Monthly = 3
}
class Challenge: RLMObject {
dynamic var title = ""
}
class TotalCountChallenge: Challenge {
dynamic var totalCountGoal: Int = 0
}
class RecurringChallenge: Challenge {
dynamic var recurranceType: RecurrenceEnum = .Daily
dynamic var totalCountGoal: Int = 0
}
When I save either TotalCountChallenge or A RecurringChallenge it does not report errors, but when I go to query for problems by name I get nothing.
Request from my ViewController
// Query using an NSPredicate object
let predicate = NSPredicate(format: "title BEGINSWITH %@", "Booya")
var challenges = Challenge.objectsWithPredicate(predicate)
if challenges == nil || challenges.count == 0 {
let tcChallenge = TotalCountChallenge()
tcChallenge.title = "Booya Total Count Challenge"
tcChallenge.totalCountGoal = 1_000_000
let rChallenge = RecurringChallenge()
rChallenge.title = "Booya Recurring Challenge"
rChallenge.recurranceType = .Weekly
rChallenge.totalCountGoal = 2_000_000
let realm = RLMRealm.defaultRealm()
// You only need to do this once (per thread)
// Add to the Realm inside a transaction
realm.beginWriteTransaction()
realm.addObject(tcChallenge)
realm.addObject(rChallenge)
realm.commitWriteTransaction()
}
challenges = Challenge.objectsWithPredicate(predicate)
if challenges != nil && challenges.count > 0 {
for challenge in challenges {
let c = challenge as! Challenge
println("\(c.title)")
}
} else {
println("No Challenges found")
}
challenges = TotalCountChallenge.objectsWithPredicate(predicate)
if challenges != nil && challenges.count > 0 {
for challenge in challenges {
let c = challenge as! Challenge
println("TotalCountChallenge: \(c.title)")
}
} else {
println("No Total Count Challenges found")
}
challenges = RecurringChallenge.objectsWithPredicate(predicate)
if challenges != nil && challenges.count > 0 {
for challenge in challenges {
let c = challenge as! Challenge
println("RecurringChallenge \(c.title)")
}
} else {
println("No Recurring Challenges found")
}
Output
No Challenges found
TotalCountChallenge: Booya Total Count Challenge
RecurringChallenge Booya Recurring Challenge
When I browse the database using the Browse tool provided by Realm I see that there is only 1 TotalCountChallenge and 1 RecurringChallenge and no problem.
Is there a way to do this?
Here is a link to the code on github: lewissk / RealmTest
source to share
Realm supports subclassing, but not the type of polymorphism you are looking for. In Realm, each type of object is stored in its own table, regardless of whether it is declared in code as a subclass of another object. The consequence of this is that it is currently not possible to query different classes of objects, even if they have a common superclass. There's an issue tracking this feature request at https://github.com/realm/realm-cocoa/issues/1109 .
source to share