Hello,
I would like to be able to create a default value on my embedded schema
I have an object definition Pet that contains a Picture object.
I would like to instantiate the Picture object when doing the realm.write(“Pet”) function
Pet Object schema :
export class Pet extends Realm.Object {
static schema = {
name: "Pet",
properties: {
_id: { type: "objectId", default: () => new Realm.BSON.ObjectId() },
name: { type: "string", default: () => "Wafme" },
age: { type: "int", default: () => 0 },
sex: { type: "bool", default: () => true },
size: { type: "int", default: () => 0 },
picture: {type:"object", objectType: "Picture"},
coordinate: "Coordinate?",
userId:{ type: "objectId"},
},
primaryKey: "_id",
};
}
But if possible i would like my pet object with following Picture definition as i think it would create a new Picture automatically but i am not able to make it work
picture: {type:"object", objectType: "Picture", default: () => { new Picture()} },
Picture object schema
export class Picture extends Realm.Object {
static schema = {
name: "Picture",
properties: {
_id: { type: "objectId", default: () => new Realm.BSON.ObjectId() },
pictureUrl: { type: "string", default: () => "https://img.freepik.com/premium-vector/cute-australian-shepherd-dog-avatar-cartoon_357749-252.jpg?w=740" },
pictureBlob: "data?",
},
primaryKey: "_id",
};
}
Part of my code
export default function MyComponent() {
const realm = useRealm();
const user = useUser();
let myPet = null
myPet = useQuery(Pet, (collection) =>
collection.filtered("userId == $0", Realm.BSON.ObjectId(user.id))
);
if (_.isEmpty(myPet)) {
realm.write(() => {
realm.create("Pet", {
userId: Realm.BSON.ObjectId(user.id),
});
});
}
>
useEffect(() => {
const createSubscription = async () => {
await realm.subscriptions.update((mutableSubs) => {
mutableSubs.add(realm.objects("Pet").filtered("userId == $0", Realm.BSON.ObjectId(user.id)), {name: 'PetSubscription'});
})};
createSubscription().catch(console.error);
}, []);
return (
<View>
<Text> Hello</Text>
</View>
)
}
1 - First Question :
The Pet object get created and written but the Picture object does not get the default value present in the schema.
What can i do to have Pet and Picture created at once during the Pet creation?
2 - Second Question :
I can update the code as following to solve my First Question, (but i want something cleaner where realm.create(“Pet”) would also create the Picture object implicitly)
But here is my second question about the code below :
Will realm.objects(“Picture”) load all the pictures i have in the collection even if i only need the one from the userID?
How can i filter this?
Will i have a lot of performance issue if i use realm.objects(“Picture”)?
I dont see a lot of example defining subscription and how they actually work, what is retrieved, or sent :
realm.write(() => {
--> const picture = realm.create("Picture", {}); // Adding this creation
realm.create("Pet", {
picture: picture,
userId: Realm.BSON.ObjectId(user.id),
});
});
.....
const createSubscription = async () => {
await realm.subscriptions.update((mutableSubs) => {
mutableSubs.add(realm.objects("Pet").filtered("userId == $0", Realm.BSON.ObjectId(user.id)), {name: 'PetSubscription'});
--> mutableSubs.add(realm.objects("Picture"), {name: 'PictureSubscription'}); //adding this line
//otherwise i get an error "Error: Cannot write to class Picture when no flexible sync subscription has been created."
})};
- Third Question :
what does truepredicate in realm.objects(“TableName”).filtered(“truepredicate”)
found New to Realm, problem creating sync subscription - #6 by d33p
Thank you for you help