Unable to save data after login
Closed this issue · 13 comments
Hello Andrew
I enabled sync in Realm, and applied the permissions stated in the accompanying article. I am able to login and the user created, however when I attempt to save an update to the profile the data does not get stored. Would you have some suggestions what I set up incorrectly?
Thank you,
Andrew
Hi @slovaka – have you gone into the Realm UI and checked the "Sync" page for your app to confirm that it's enabled? If it is then I'd check the Realm logs (again using the Realm UI).
Cheers, Andrew.
Hi @am-MongoDB thanks for writing back. I checked the logs as you suggested but do not see any errors. What I have been noticing though is that in the sync section of realm site I saw this error today: "Sync has stopped between Atlas and Realm. Fix this issue by attempting to restart sync for Atlas." I restarted it, re-tried and after a while saw the same message again.
I upgraded to Realm SDK to 10.5.1 in swift package manager. I am on an M1 Mac.
Appreciate your help looking forward to resolving :)
Regards,
Andrew
@slovaka When restarting the sync, there used to be an option to select whether or not to use the "resume token" and I found that restarting sync would sometimes fail if I didn't deselect it. It looks like that option has been removed. I'd suggest doing this to start the sync from scratch:
- Take a note of the sync settings (including the permissions)
- Click the "Terminate Sync" button
- Add back in the sync details and start the sync
If sync fails then there should be an associated log.
Hi @am-MongoDB When I tried to restart the sync, I get the error message "trigger not found: '6010ea43aa5bdccded8f5c9d'" Perhaps I should start everything from the beginning.
That implies that. you've terminated sync (it doesn't automatically clear the banner with the restart button). After terminating the sync, you need to add the sync config stuff back in as if you were setting it up for the first time.
I re-added sync config (I did attempt this many times before.. I only was able to login when enabling development mode), and followed your instructions using user as key and inputting the following permissions, but I am now getting an error in the logs:
Permissions:
{
"%%true": {
"%function" : {
"arguments" : [
"%%partition"
],
"name" : "canReadPartition"
}
}
}
{
"%%true": {
"%function" : {
"arguments" : [
"%%partition"
],
"name" : "canWritePartition"
}
}
}
Log Entry:
Error:
could not evaluate sync permissions with error: function not found: 'canWritePartition' (ProtocolErrorCode=206)
Partition:
user=6012dbbd6de82f92496f1e2d
Remote IP Address:
XXX.XX.250.100
SDK:
Realm Cocoa v10.5.1
Platform Version:
Version 14.3 (Build 18C61)
Did you import the Realm app? That should have set up the canReadPartition and canWritePartition functions?
Yes, I did but I will start over from scratch to make sure I did not make a mistake in the process initially.
I started from scratch and did the import and I now do see data syncing in Atlas to Chatster and User collections which is great. When I try to save the profile after choosing an image and giving a display name in the simulator, in the debugger state.user is nil. on this line. I used title as the key. I got the same error when I set up user as the key:
state.user?.userPreferences?.avatarImage = newPhoto
On the server I am seeing this error
Error:
Failed to apply MongoDB change event to sync history
Source:
Error syncing MongoDB write
Logs:
[
"Change Event Type: insert",
"Namespace: RChat.Chatster",
"Partition: ",
"Document ID: 60136876a34c28bf0d79c06a",
"Detailed Error: full document for MongoDB change event didn't contain required partition key "title" field"
]
I am very interested to get this set up and try out what is a really great capability with realm and sync, and appreciate your help.
The partition key should be configure as "partition", and then the values you assign to the partition field are key value pairs like "partition = "user=4878978973487568". The error is suggesting that you've configured "title" as your partition key in your backend Realm app.
I'm hoping to publish a new article on Tuesday that will give more details about how to set things up and how the app works with Realm.
That would be great Andrew regarding more details. I terminated sync and added:
partition = "user=4878978973487568"
in the partition key text field, however it did not work for me. I will keep trying and re-read both your article and the realm documentation once again.
I was able to solve the issue by using the partition key for sync as
partition: "all-users=all-the-users"
I set it as an object
I looked at the Chatster collection in Atlas and noticed this partition key and value there already. Once I set that in Sync I was able to login, save profile and everything worked great!
Tutorial is now live at https://developer.mongodb.com/how-to/building-a-mobile-chat-app-using-realm/