-
Type: Bug
-
Resolution: Gone away
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
What happened?
While using PopulateInitialSubscriptions (on clean, fresh start without a database file), on attempt to write a new object, Realm throws RealmException: Cannot write to class <ClassName>when no flexible sync subscription has been created..
It happens when using models with MapTo attribute, without the attribute everything works fine.
Repro steps
- Try to connect with PopulateInitialSubscriptions and a model with MapTo attribute
- Exception
Solution: Removing MapTo and renamed the object model from LanguageEntity to Language solved the problem.
Although I made it work, it's inconvenient to work without MapTo, please take a look.
Version
.NET 6
What Atlas Services are you using?
Atlas Device Sync
What type of application is this?
Other
Client OS and version
Windows 10
Code snippets
Use this code to connect:
_app = App.Create(new AppConfiguration(myRealmAppId) { BaseFilePath = FileService.AppDataDirectory, }); _user = await _app.LogInAsync(Credentials.Anonymous()); _config = new FlexibleSyncConfiguration(_user) { PopulateInitialSubscriptions = (realm) => { realm.Subscriptions.Add(realm.All<LanguageEntity>()); } }; var _realm = Realm.GetInstance(_config); await _realm.Subscriptions.WaitForSynchronizationAsync(); _realm.Write(() => { var languages = _localRealm.All<LanguageEntity>(); foreach (var language in languages) { _realm.Add(new LanguageEntity() { Code = language.Code, _id = language._id }); } }); await _realm.Subscriptions.WaitForSynchronizationAsync();
With this model:
namespace Data.Entities { [MapTo("Language")] public class LanguageEntity : RealmObject { [PrimaryKey] public ObjectId _id { get; set; } = ObjectId.GenerateNewId(); public string Code { get; set; } = string.Empty; public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.Now; } }
And if Realm.GetInstance() is changed to await Realm.GetInstanceAsync() - it just hangs there indefinitely with these logs (after which it’s just ping-pong logs:
Stacktrace of the exception/crash you're getting
RealmException: Cannot write to class Language when no flexible sync subscription has been created.
Relevant log output
APP: Realm : 2023-01-13 04:19:35.526 Debug: WebSocket::initiate_client_handshake() APP: Realm : 2023-01-13 04:19:35.758 Debug: WebSocket::handle_http_response_received() APP: Realm : 2023-01-13 04:19:35.760 Detail: Connection[1]: Negotiated protocol version: 7 APP: Realm : 2023-01-13 04:19:35.761 Debug: Connection[1]: Will emit a ping in 12788 milliseconds APP: Realm : 2023-01-13 04:19:35.763 Debug: Connection[1]: Session[1]: Sending: IDENT(client_file_ident=18, client_file_ident_salt=576499042362797227, scan_server_version=12, scan_client_version=5, latest_server_version=12, latest_server_version_salt=4481538362546054629, query_version: 0 query_size: 2, query: “{}”) APP: Realm : 2023-01-13 04:19:35.765 Debug: Connection[1]: Session[1]: Sending: MARK(request_ident=2) APP: Realm : 2023-01-13 04:19:36.140 Debug: Connection[1]: Session[1]: Received: MARK(request_ident=2) APP: Realm : 2023-01-13 04:19:36.142 Debug: Connection[1]: Session[1]: Sending: UPLOAD(progress_client_version=13, progress_server_version=12, locked_server_version=12, num_changesets=0) APP: Realm : 2023-01-13 04:19:49.259 Debug: Connection[1]: Sending: PING(timestamp=133122415, rtt=0) APP: Realm : 2023-01-13 04:19:49.321 Debug: Connection[1]: Received: PONG(timestamp=133122415) APP: Realm : 2023-01-13 04:19:49.326 Debug: Connection[1]: Round trip time was 67 milliseconds APP: Realm : 2023-01-13 04:19:49.327 Debug: Connection[1]: Will emit a ping in 55078 milliseconds The thread 0x759c has exited with code 0 (0x0). The thread 0x7560 has exited with code 0 (0x0). The thread 0x7004 has exited with code 0 (0x0). The thread 0x29cc has exited with code 0 (0x0). The thread 0x675c has exited with code 0 (0x0). APP: Realm : 2023-01-13 04:20:44.900 Debug: Connection[1]: Sending: PING(timestamp=133178056, rtt=67) APP: Realm : 2023-01-13 04:20:44.962 Debug: Connection[1]: Received: PONG(timestamp=133178056) APP: Realm : 2023-01-13 04:20:44.964 Debug: Connection[1]: Round trip time was 64 milliseconds APP: Realm : 2023-01-13 04:20:44.965 Debug: Connection[1]: Will emit a ping in 55907 milliseconds