Reputation: 327
Generic question about integrating Realm with an existing project. Another framework I have has a User
class which has a bunch of properties. Is there anyway with Realm that I can just store this object without creating an entire new Realm model and copying over the values from the User
's properties to the new Realm model?
Can I do something like this?
class RealmUser: Object {
dynamic user: User?
}
Even though user isn't a Realm defined model.
Thanks.
Upvotes: 1
Views: 1013
Reputation: 27620
Realm only supports the following property types: Bool
, Int8
, Int16
, Int32
, Int64
, Double
, Float
, String
, NSDate
, and NSData
.
So you cannot simply add your User
object to a Realm. If your User
class implements NSCoding
you could convert the User
object to a NSData
object and store that, but you would lose the ability to query User
's properties so IMHO that is not a practical option.
If you really want to use Realm I think there is no other way but to create a RealmUser
object with all the properties from the User
class. You can add a convenience initializer that makes the creation of RealmUser
objects easier in other parts of your code base:
class RealmUser: Object {
dynamic var userId = 0
dynamic var name = ""
...
convenience init(withUser user: User) {
self.init()
userId = user.id
name = user.name
...
}
}
This is not as short and practical as your solution would be, but adding custom classes to Realm is just not possible.
Upvotes: 2