Offline access to read only full realm


#1

Realm Swift 3.6

I have an Realm platform based application with two realms. One is the standard user realm and the second is a read only realm that is shared across users. After the user logs in for the first time, the shared realm is fully synced (and always opened via asyncOpen as described in the docs) to the device before the user is allowed to continue any further into the app. From this point onward, the data within the shared read-only realm is accessible and the application works as desired as long as the user remains online or the app does not close. However, as this is unrealistic to expect, I have tried a range of different situations and here are my results:

  1. If the user is online and the app is started up from a closed state (either after having been closed due to to inactivity or killed by the user) everything works as expected and the shared data is available.
  2. If the app has not been closed and the user goes offline, all the shared data is accessible.
  3. As soon as the user goes offline and tries to start up the application from a fully closed state (again I am using asyncOpen behind the scenes), the shared data is completely missing, but it will become accessible when the app is brought back online either from a closed state or while the app in open.
  4. If I make the shared realm read/write, all these problems go away.
  5. If I build directly to a device via Xcode, run the app while online, close it and then build again to the same device that has now been taken off line, even through the app is offline the shared data is accessible. I can kill the app and open it as many times as I want while it is off line and the shared data will always be there. However, as soon as that build is brought online (shared data is visible still), killed and then opened again when off-line, the shared at a is again gone.

This use of shared read only data seems to be a pretty simple and I would think standard use case. Fundamentally, I would expect the data in the shared realm to be available on the device even if the user is offline and even if it is read only data. While one potential solution is to make the data read write, I don’t like this approach because any user with a malicious streak could log into the serve using their own credentials and delete all the data. I have also read that partial sync might be a possible solution, but given the fact that I want the user to have the full copy fo the shared data on their local device at all times, a full synced realm seems the correct approach.

Am I missing something here or is there a bg in how the synced realms that are read only work?


#2

@robowen5mac This should not be the case if the realm is already downloaded onto the device. Are you logging the user out via the SyncUser.logOut() API? Do not use this if you are trying to support the use case of opening the realm while offline - this deletes the data and must be re-downloaded.

How are you applying permissions for read-only? Please use our Fine-grained permissions to apply read-only instead of the path-level described here:
https://docs.realm.io/platform/using-synced-realms/access-control#fine-grained-permissions

If you have followed the above then Please open a ticket at support.realm.io and we will get you sorted


#3

Thanks for the reply.

No, the user is still logged in.

I am currently using the historical path based approach as the initial code was written a while a go and I didn’t appreciate that a change might be required to the new fine grained controls. My current workflow is that I have a separate application that creates the shared realm and writes it to the server at a specific path. At the end of the process, a path based permission is then applied to all users by passing a “*” and .read permission. Are you saying that the path based mechanism does not work in this case and would explain the results I am seeing?

I thought the finer grained controls were meant for partially synced realms, which I am not using for this particular shared realm. As a result, I don’t think they will work as things stand.


#4

@robowen5mac You can still have Path-based permissions under our new fine-grained system which is new and improved - see here:
https://docs.realm.io/platform/using-synced-realms/access-control#fine-grained-permissions

I don’t know enough about your situation to determine if that is what is going on. Best to up the logging to trace and open a ticket.


#5

OP is probably long gone, but I’m having the same issue. Was this resolved by changing to the Fine-grained permissions for a universally readable realm? Applying path-level permissions is deprecated?


#6

Nope…still here, just didn’t see there was more traffic on the issue, sorry. I am using the full sync permissions as I am not currently using the query based approach (link above). For me the full sync approach is still the best for our application.

Things are still working but I have had to leave the shared realm read/write. I never found a solution that gave me what I wanted after talking to support. It should work, but in my cause did not. I found that my data model was so interconnected, the items in my user only realms which required read write, were also getting created as part of the realm that I wanted to be read only. I think, since the two were not completely separate entities with Objects that only existed in one or the other, this messed things up and caused the problem I found.

Honestly, I kinda just gave up on things and I had larger issues to deal with and could enforce read only in my code. Not idea, but seems to be meeting our needs for now. Sorry I can’t provide better news.


#7

Thanks for the reply. I was meaning to add some information I found in the docs:

If a Realm has read-only Realm level permissions, then you must asynchronously open the Realm as described in Asynchronously Opening A Realm. Opening a file-level read-only Realm without the asynchronous API will cause an error.

Async opening will hang if the app is offline. So, that seems to be the answer. Not sure why it should be that way, but its the official behavior. I too am ok with it - there wasn’t much benefit to having a read-only realm alongside my main partial sync realm. I’m using one realm now too and I’m happy.