I’ve seen that a supported type for a list inside a realm is “RealmList”, is that correct?
Now in my android application, I have a class that contains a property of a type List. So to be able to use a List inside a Realm, I need to change that type to a RealmList?
Now my questions is, how can I specify a default value for a RealmList?
Not exactly… ? A List is a property of a Realm Object. To use a List, you would need to add a List property to an object. The object is then stored in Realm. To get to that list, you would need to query/load that object and then work with it’s List property
A good example is a situation where there is a person object and a dog object. A person can have meny dogs - psuedo-code:
Person Object {
RealmList myDogs = several DogObjects
}
So to see a persons dogs, load the person, then you can get the list of dogs.
There’s a lot more to this with inverse-relationships, embedded objects etc. Going through the Getting Started Guide would provide additional information.
Hey Jay, thanks for answering to my question. I’ve found one function named: realmListOf() which does creates a list of a type RealmList. Which is great! Now my question is, how can I represent that RealmList object in the schema of a realm?
This is how my current schema looks like. And for now I’ve specified a list to have a string type, until I figure out how to specify a list of multiple strings in that same schema:
A RealmList is treated very much as an array. So a RealmList of type String would essentially be an array of strings. You can have an array of primitives like string, or an array of managed or embedded objects as well.
For example, in a Chat Room style app, there may be an object that represents a Chat Room with a Realm List property called userIdList of the users in that room.
The object that contains the userId (and other info) object looks like this
UserIdClass
users_id which is a String
The parent property is
ChatRoom
userIdList which is a RealmList of UserIdClass objects
If this is a synched app and the app is in Developer Mode on the Realm Console, the objects you build in code in the SDK will be created in Atlas automatically. e.g. if you craft an object in the SDK that has a RealmList property, then when the app sync’s that object and schema will be created in Atlas.
I’ve just tried using the approach with Development Mode, and indeed a new collection schema was created automatically from the model class from my app. However that schema contained only the name of the model class, and none of the properties.
I have gone through the getting started guides numerous times and while they are a little thin, they are accurate so that documentation Atlas Device Sync contains everything you need to set your app up on the site in development mode.
Next, under the Development Mode subheading, you can choose to toggle Development Mode on/off. Enabling Development Mode allows you to define schemas directly in your client application code and is suitable if you are in development and do not have application data in Atlas yet.
The second thing is that if either a) the above guide was not followed or b) the Model is not correctly set up - either or both of those will cause the model to not show up in Atlas correctly.
So please go through the guide and also post your model here (the actual model code from your app)
Yeah I’ve read the docs couple of times, but they are missing a lot of practical things, especially for beginners who’re just getting started with the SDK. Nevertheless, I’ll go though the whole process once again. Here’s my model class from the code. Thank you for your responses Jay, really helpful.
open class Diary : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId.create()
var ownerId: String = ""
var mood: String = Mood.Neutral.name
var title: String = ""
var description: String = ""
var images: String = ""
var date: RealmInstant = RealmInstant.from(System.currentTimeMillis(), 0)
@RequiresApi(Build.VERSION_CODES.O)
var localDate: LocalDate =
Instant.ofEpochMilli(date.epochSeconds).atZone(ZoneId.systemDefault()).toLocalDate()
}
Btw, here you can see that I’m using a RealmInstant type for the date. However I’ve been having troubles converting a LocalDateTime type into a RealmInstant. I’ve been getting a wrong format when trying to make a conversion, check out my question on stackoverflow here. I’ve added a bounty
One other thing - you may have some kind of sync error, so check your console logs
Realm console->App services tab->Click you app->View all logs activity
While the errors shown there (if any) are usually cryptic and you’ll need a Rosetta stone to decipher what the error actually means, at least you’ll know if you’re getting errors and some direction as to what they are.