Blank local sync default file

I am using device flexible sync on App UI as per Swift examples. The correct roles, permissions and filters are in place. I use anonymous credentials to login and to add a flexible sync configuration. I’ve added a subscription. The logs on the App UI show a connection starting, a session starting, and the status is okay. But there is nothing on my local realm sync default file. What am I missing?

Create realm sync function

    let app = App(id: RealmConstants.realm_App_ID)
    let  users = try await app.login(credentials: Credentials.anonymous)
    var config = user.flexibleSyncConfiguration()
    config.objectTypes = [AccessibleItem.self]
    let realm = try await Realm(configuration: config, downloadBeforeOpen: .once)
    let subscriptions = realm.subscriptions
    let foundSubscription = subscriptions.first(named: "all_access_items")

    if foundSubscription != nil {
        print("found the subscription: \(String(describing: foundSubscription?.name))")
    } else {
        try await subscriptions.update {
            subscriptions.append(
                QuerySubscription<AccessibleItem>(name: "all_access_items")
    
            )
        }
    }
    return realm

Rules

{
  "name": "Anyone",
  "apply_when": {},
  "document_filters": {
    "read": true,
    "write": false
  },
  "read": true,
  "write": false
}

Document filters

{
  "place": {
    "$exists": true
  },
  "address": {
    "$exists": true
  }
}

Local realm file

Thanks.

Hi, is there any data that should be added to the file? IE, are you writing any objects to the Realm or is there any data in Atlas that should be getting synced? If so, can you explain where the data is and perhaps add a link to your application in the App Services UI? Separately, can you clarify what your document filters are set to? You seem to suggest it is both { read: true, write: false } as well as { place: { $exists: true } , address: {$exists: true } }

Hi Tyler,

Thanks for getting back to me so quickly.

I want to sync the data on Atlas with the local Realm file. Currently the user cannot write to the file. This could change in the future. The collection is on a shared cluster tier, the atlas service name is mongodb-atlas.

Is this the link you are looking for ? App Services

From what I understand of the filters for flexible sync, there needs to be a document filter for the read property, in order for the role to be sync compatible. That’s why I added more detailed filters. Have I misunderstood the requirements?

Hi,

So I would start off with a few things.

First off, you seem to have misunderstood the “apply when” section as you have defined many of the other rules-related fields within the apply when. As it is written now, the apply_when will fail because of this and therefore the sessions will connect with a “no access” role. I would suggest reading through this page: https://www.mongodb.com/docs/atlas/app-services/rules/roles/#roles

However, my meta comment would be that I generally advise people to develop their application without permissions first, make sure things are working, tune your data model, and then try to figure out how to add permissions in on top of what you have. So I would suggest removing that rule, adding a new one with a default “everyone can read” rule, and then continuing to build your app. Once you have things generally working, I would advise you to start thinking about rules and permissions. This will make your development experience a lot better.

Thanks for the good advice.

I’ve set the rule to everyone can read. I deleted the sync default realm file and also changed

    let realm = try await Realm(configuration: config, downloadBeforeOpen: .once)

to

    let realm = try await Realm(configuration: config, downloadBeforeOpen: .always)

before starting up again.

It is now working.

Awesome. I would continue to build and put things together and once you have something resembling a working prototype I think you will have more context around what your permissioning needs to be and then you can go add it.

Enjoy,
Tyler

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.