I am new to Maui and Realm, so please bear with me.
I have now been playing around with Realm for some days and I’m able to save local on the device.
Next step is now to sync the data to the cloud, but I have some problems with that.
I try to follow this Youtube video from Luce Carter, to see if that will work for me, but I am getting a No User Object error, can somebody help me?
Hey Jonas, can you post some code around where the error occurs as well the actual error you’re getting along with the stacktrace. That’ll help us greatly when identifying what the issue may be.
I can try, here is the code that’s failing, the user object is not being defined
public async Task InitialiseRealm()
{
config = new PartitionSyncConfiguration($"{App.RealmApp.CurrentUser.Id}", App.RealmApp.CurrentUser);
realm = await Realm.GetInstanceAsync(config);
Console.WriteLine(realm.All<User>());
user = realm.Find<User>(App.RealmApp.CurrentUser.Id);
if(user == null)
{
await Task.Delay(5000);
user = realm.Find<User>(App.RealmApp.CurrentUser.Id);
if(user == null)
{
Console.WriteLine("NO USER OBJECT: This error occurs if " +
"you do not have the trigger configured on the backend " +
"or when there is a network connectivity issue. See " +
"https://docs.mongodb.com/realm/tutorial/realm-app/#triggers");
await App.Current.MainPage.DisplayAlert("No User object",
"The User object for this user was not found on the server. " +
"If this is a new user acocunt, the backend trigger may not have completed, " +
"or the tirgger doesn't exist. Check your backend set up and logs.", "OK");
}
}
This app only shows an example of “front-end” code to show what kinds of things you can do with Realm and MAUI.
The users are created from back-end code using Atlas App Services Triggers and Functions.
If you wanted to actually replicate the whole thing yourself, you would also need to set that up too.
The tutorial I followed has been deprecated now in favour of using Templates in App Services but I will make two GitHub gists tomorrow and share the code I added
Make a new AppService with the Build your own app template and name it HouseMovingAssistant
Create functions
Replace HouseMovingAssistantCluster with HouseMovingAssistantDB
Create trigger
Insert the app ID in your app
Authentication
Go to Authentication and Authentication Providers Select Email/Password and the the following:
Provider Enabled: Enabled
User Confirmation Method: Automatically confirm users
Password Reset Method: Send a password reset email
Password Reset URL: https://yoururl
Enable deviceSync
Go to Device sync and Enable sync, press No thanks, continue to Sync and use the following settings
Sync Type: Partition based
Development Mode: Enabled
Select a Cluster to Sync: “Your Cluster”
Define a Database Name: HouseMovingAssistantDB
Choose a Partition Key: _partition
Field Type: String
Required Field: Enabled
Create an index on the partition key: Enabled
Define Permissions: Users can only read and write their own data
The first time i started the app it was failing, but i think it is because the schema isn’t in place
I seem to have a problem with one line of code that is messing up the rest of my test of this app.
user = realm.Find(App.RealmApp.CurrentUser.Id);
The user property ends up null. I find this a pretty interesting example since using MVVM and the community tool kit. Has a nice simple, readable code, but that line?
I started my learning of Xamarin with Realm/Atlas with the Template for xamarin. Got it working just fine. But what led me to your example was as soon as I tried to use a partition, which I have a feeling is the best way to sync, the template example went south! I played around trying to get it working and just was missing things.