Crash subscribing to partial realm

I am facing a SIGABRT and an application crash while running the following code.

This was functional until the last update (3.15.0) and server version .3.21.1

var config = SyncUser.current?.configuration(realmURL: URL(string: “MY_VALID_URL”), fullSynchronization: false, enableSSLValidation: false)
self.realm = try Realm(configuration: config!)
self.results = self.realm!.objects(MY_CUSTOM_CLASS_OBJECT.self)
self.subscription = self.results!.subscribe()

CRASH

the realm url has been checked and is valid

The content of the self.results variable has been also checked and is not nil

Thanks for advice

Please, dont use force unwrap.
What happen in your case, if the SyncUser.current is nil? Where do you catch the realm errors?

With proper error handling and using guard, you can prevent these issues.

Thanks for your prompt answer freeubi

you are right if the SyncUser.current is nil

I have guard statement above the code

guard let _ = SyncUser.current else {
completion(false)
return
}

But even with proper error handling i get the CRASH at the line where i create the subscription

let subscription = self.results.subscribe() <= CRASH HERE

Is your realm an upgraded reference realm?

yes it has been updated.

I have also tried Realm.asyncOpen :

guard let current = SyncUser.current else {return}

var config = current.configuration(realmURL: URL(string: "realms://MY_REALM_URL"), fullSynchronization: false, enableSSLValidation: false)

config.objectTypes = [MY_CUSTOM_OBJECT]

Realm.asyncOpen(configuration: config) { (realm, error) in
    if let realm = realm {
        let result = realm.objects(Customer.self)
        self.subscription = result.subscribe()
    }
}

I still get the SIGABRT -> self.subscription = result.subscribe()

I have the same problem.

let realm = try! Realm()
let attachedfileNotificationToken = realm.objects(AttachedFile.self).filter("status == None").subscribe() 

Your code works for me. Your Syncuser could be nil or your config may not be correct or any number of other things could be causing an issue. Can you include a more complete, and minimal example? And indicate what line your code is crashing on and what steps you done to troubleshoot the issue.

The app starts with setupRealm in a loginVC to login to the server, and after that I use subscribe(). There is no nil there in every step.
I get " libc++abi.dylib: terminating with uncaught exception of type NSException"



func setupRealm(_ creds: SyncCredentials, completion: @escaping (_ realm: Realm?, _ error: Error?)->Void) {
            
        SyncUser.logIn(with: creds, server:  APIConstants.cloudSyncAuthURL) { [weak self] user, error in
                guard
                    let user = user else {
                        print (String(describing: error.debugDescription))
                        DispatchQueue.main.async {
                            self?.defaultAlertController(message: String(describing: error.debugDescription), confirmButtonTitle: "OK")
                        }
                        completion(nil, error)
                        return
                }
                DispatchQueue.main.async {
                    // Create the configuration
                    let config = user.configuration(realmURL: APIConstants.cloudRealmURL, fullSynchronization: true)
                    // Open the remote Realm
                    APIConstants.synConfig = config
                    Realm.Configuration.defaultConfiguration = config
                    print("Successfully connected to realm!")
                    completion(try? Realm(configuration: config), nil)
                    self?.initVc()
                }
            }
    }


if SyncUser.current != nil {
            let realm = try! Realm()
            let subc = realm.objects(AttachedFile.self).filter("status == 'None'").subscribe() <<====== Crash
            self.attachedFileNotificationToken = subc.observe(\.state, { (state) in
                switch state {
                case .creating: break
                        // The subscription has not yet been written to the Realm
                case .pending: break
                        // The subscription has been written to the Realm and is waiting
                        // to be processed by the server
                case .complete:
                    print("Sync completed.")
                        // The subscription has been processed by the server and all objects
                        // matching the query are in the client Realm
                case .invalidated: break
                        // The subscription has been removed
                case .error(let err):
                    print("\(err)")
                        // An error occurred while processing the subscription
                }
            })
        }

You’re opening a full Realm and trying to create a subscription which is an API that only works with query based sync. I’m surprised that the error message in that exception doesn’t state that.

Another question, now I have full-sync realm and I want to share a specific object like ‘’ let obj = realm.object(ofType: type, forPrimaryKey: id), and I don’t want to share the whole realm instance “/~/UserRealm”, what is the best scenario in this situation? I have read realm.doc but I get confused because u recommend full-sync.
Thanks.

In subscription regards, how could we know that the image object get uploaded to the server in a full-sync Realm?

You can’t share parts of the full Realm - it’s either the entire Realm or nothing at all.

Props to @nirinchev for being on point.

The questions you’re asking would probably be better answered if we understood what you’re trying to do. You could have an inherent architecture issue or there may be some other way of using your data.

Generally, you will have separate realms for each user/group/entity and if you need to share data, that would be through a global realm.

Query (Partial) based sync Realms are ‘going away’ at some point which is why you will see over and over the documentation points to using full-sync realms. (or they could re-engineer it. We’ll see)

Query-based Sync is not recommended. For applications using Realm Sync, we recommend Full Sync. Learn more about our plans for the future of Realm Sync here.

To address the second question:

In subscription regards, how could we know that the image object get uploaded to the server in a full-sync Realm?

There are no subscriptions in Full Sync Realms. You have access to all of the data, all of the time. If you store an item in Realm, like an image, it’s written locally first and then at some point thereafter it’s written to the server. If another client is observing a results object, that event will fire once the data is on the server and available.

If you want to monitor progress of Realm, there are Progress Notifications that can be used for example, to update your UI during a long process.

Thank you Jay, in sharing regards (full-sync), if i understand that right, I will create a global Realm for USERS that stores user info ex. (name, email etc.) and it will be accessed by all users.
I don’t know what senario should i take but I have 2 suggestion, correct me if I am wrong.
1- Every user has his/her own “/~/UserRealm” and if a user1 need to share data with user2, user1 should create /~/UserRealmShare and copy the shared object on it “let obj = realm.object(ofType: type, forPrimaryKey: id)” and then give a permission to user2 read/write.
2 - Every user has his/her own “/~/UserRealm” and if a user1 need to share data with user2, user1 should create another global Realm “/GlobaRealm” and copy the shared object on it “let obj = realm.object(ofType: type, forPrimaryKey: id)” and grant just user2 permission, that means every shared object has to have its own “/GlobaRealm” and the owner (user1) grants just a specific user (user2) not all users a permission.
Thanks in advance.