Realm Object Server creating DB and ROS but not syncing - ios

I have set up a Realm Object Server on AWS (RHAT) and it appears to be working fine. I can look at the dashboard which tells me how many connections, and how many realms open on port 9080. My code is working fine on the device, updates are happening on the devices realm. But I dont see anything changing in the ROS realm. I may be getting something very basic wrong, but I can't tell what.
func setupRealm() {
// Authenticating the User
let username = "exampleuser"
let password = "examplepassword"
SyncUser.logInWithCredentials(SyncCredentials.usernamePassword(username, password: password), authServerURL: NSURL(string: "http://example.com:9080")!, onCompletion:
{ user, error in
if let user = user {
// Opening a remote Realm
let realmURL = NSURL(string: "realm://example.com:9080/~/VWF3")!
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: realmURL))
let realm = try! Realm(configuration: config)
// Any changes made to this Realm will be synced across all devices!
} else if let error = error {
// handle error
}
})
}
I call setupRealm() from ViewDidLoad.
My developers need to be able to see the changes to the ROS realm to ensure all is working correctly, but all that is there is the single users realm, structure of tables is accurate, zero data.
I checked the log files on the server and nothing stood out.
Matt

You need to make sure you're still referring to that particular Realm (and its sync configuration) when doing your writes. If you tried to do a write to let realm = try! Realm() even after this login, you'd still be referencing your default Realm, which isn't synchronized.
If you plan to make your entire app synchronized, you can set that particular Configuration as your default Realm's one:
if let user = user {
// Opening a remote Realm
let realmURL = NSURL(string: "realm://example.com:9080/~/VWF3")!
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: realmURL))
Realm.Configuration.defaultConfiguration = configuration
// All calls to `Realm()` will now use this configuration
}
Otherwise you can always just generate the configuration when you need it (Or have a global constants class that can manage it for you).
let realmURL = NSURL(string: "realm://example.com:9080/~/VWF3")!
let configuration = Realm.Configuration()
configuration.syncConfiguration = SyncConfiguration(user: SyncUser.current, realmURL: realmURL))
let realm = try! Realm(configuration: configuration)

Related

Realm iOS: How to handle Client Reset

Basically, I want to handle a case where any device got SyncError with type ClientResetError then, want my device to re-login to realm again. but as per documentation, we have to closeRealmSafely before I login to realm again, but I am not sure how to close realm safely.
I am going through the doc (https://docs.realm.io/sync/using-synced-realms/errors#client-reset) to handle client reset error and found it's very confusing . I want help to understand about the following code.
First there is no method available to closeRealmsafely. Please help me understand how can I close the realm safely?
How can I backup and when I will use it? Should I skip the reset error because in documentation it's mentions if the client reset process is not manually initiated, it will instead automatically take place after the next time the app is launched, upon first accessing the SyncManager singleton. It is the app’s responsibility to persist the location of the backup copy if needed, so that the backup copy can be found later."
Below is the error handler sample code from the doc.
let syncError = error as! SyncError
switch syncError.code {
case .clientResetError:
if let (path, clientResetToken) = syncError.clientResetInfo() {
closeRealmSafely()
saveBackupRealmPath(path)
SyncSession.immediatelyHandleError(clientResetToken)
}
default:
// Handle other errors...
()
}
}```
Finally we figured out how to handle the client reset error. We have taken following steps To avoid the data loss incase user is offline and came online and got reset error.
Save the local realm to another directory
Invalidate and nil the realm
Initiate realm manual reset - Call SyncSession.immediatelyHandleError with clientResetToken passed and it will delete the existing realm from directory
Show client reset alert - This will intimate user to relaunch the app.
On next launch realm creates a fresh realm from ROS.
After new realm connects, restore the realm records (if any) from the old realm saved in backup directory above.
Delete the backup realm(old realm) from directory.
switch syncError.code {
case .clientResetError:
if let (path, clientResetToken) = syncError.clientResetInfo() {
// taking backup
backUpRealm(realm: yourLocalRealm)
// making realm nil and invalidating
yourLocalRealm?.invalidate()
yourLocalRealm = nil
//Initiate realm manual reset - Call `SyncSession.immediatelyHandleError` with `clientResetToken` passed and it will delete the existing realm from directory
SyncSession.immediatelyHandleError(clientResetToken)
// can show alert to user to relaunch the app
showAlertforAppRelaunch()
}
default:
// Handle other errors...
()
}
}```
The back up realm code look like this:
func backUpRealm(realm: Realm?) {
do {
try realm?.writeCopy(toFile: backupUrl)
} catch {
print("Error backing up data")
}
}
After doing this backup will be available at backup path. On next launch device will connect and download a fresh realm from ROS so after device connects restore the realm records from the backup realm saved in the backup path.
The restore merge backup code will look like this. place the below method when realm connects after relauch.The ```restoredRealm`` is fresh downloaded realm on launch
func restoreAndMergeFromBackup(restoredRealm: Realm?) {
let realmBackUpFilePath = isRealmBackupExits()
// check if backup exists or not
if realmBackUpFilePath.exists {
let config = Realm.Configuration(
fileURL: URL(fileURLWithPath: realmBackUpFilePath.path),
readOnly: true)
let realm = try? Realm(configuration: config)
guard let backupRealm = realm else { return }
//Get your realm Objects
let objects = backupRealm.objects(YourRealmObject.self)
try? restoredRealm?.safeWrite {
for object in objects {
// taking local changes to the downloaded realm if it has
restoredRealm?.create(YourRealmObject.self, value: object, update: .modified)
}
self.removeRealmFiles(path: realmBackUpFilePath.path)
}
} else {
debug("backup realm does not exists")
}
}
private func isRealmBackupExits() -> (exists: Bool, path: String) {
let documentsPath = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let realmPathComponent = documentsPath.appendingPathComponent("your_backup.realm")
let filePath = realmPathComponent.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
return (true, filePath)
}
return (false, "")
}
private func removeRealmFiles(path: String) {
let realmURL = URL(fileURLWithPath: path)
let realmURLs = [
realmURL,
realmURL.appendingPathExtension("lock"),
realmURL.appendingPathExtension("realm"),
realmURL.appendingPathExtension("management")
]
for URL in realmURLs {
do {
try FileManager.default.removeItem(at: URL)
} catch {
debug("error while deleting realm urls")
}
}
}```
In our testing we have found that there is a backup made by realm automatically so we deleted it for safety purpose. the path argument you will get in the if let (path, clientResetToken) = syncError.clientResetInfo()
func removeAutoGeneratedRealmBackUp(path: String) {
do {
try FileManager.default.removeItem(at: URL(fileURLWithPath: path))
} catch {
debug("error while deleting realm backUp path \(path)")
}
}

In swift, is there a way to add new data from second Realm bundle, without overwriting any existing data in the current Default Realm?

Upon initial load of the app, the Bundled Realm (Realm1) is copied to the documents folder. Now that the bundled realm is set as the default realm, I am able update the bool property so that the table view can show marked and unmarked cells. However I am looking for a way to bundle a second realm (Realm2) with a later update, that will add new data to the existing default realm, but without overwriting the current default realm. I am currently working in swift 5 and Xcode 11.1, if that is helpful.
So far the only thing that I can think of is adding block of code to add new entries to the default realm. First the view will check to see what the count is of the realm, and if the count is the same as the original bundle, then it will add new data, if the count is equal to the initial bundle plus the new entries, then it will not add the new data again. I was hoping for a simpler solution that is cleaner in my opinion.
Ideally the end result would be a way to update the existing default realm, without overwriting the already edited content. Although I am rather new to using realm, any help in pointing me in the right direction for a solution would be greatly appreciated. Thanks.
Attached below is the current code I have implemented to load the default realm from the bundle.
let bundlePath = Bundle.main.path(forResource: "preloadedData", ofType: "realm")!
let defaultPath = Realm.Configuration.defaultConfiguration.fileURL!.path
let fileManager = FileManager.default
// Copy Realm on initial launch
if !fileManager.fileExists(atPath: defaultPath){
do {
try fileManager.copyItem(atPath: bundlePath, toPath: defaultPath)
print("Realm was copied")
} catch {
print("Realm was not coppied \(error)")
}
}
return true
Once you've created your default Realm on disk, if you want to read data from the bundled one, here's the code
let config = Realm.Configuration(
// Get the URL to the bundled file
fileURL: Bundle.main.url(forResource: "MyBundledData", withExtension: "realm"),
// Open the file in read-only mode as application bundles are not writeable
readOnly: true)
let realm = try! Realm(configuration: config)
and once you've read the data, you can switch back
var config = Realm.Configuration()
config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("\(some_realm_name).realm")
Realm.Configuration.defaultConfiguration = config
as far as not overwriting, ensure your objects use a unique primary key and when they are written, nothing will be overwritten as objects will a unique primary key will be added instead of overwriting.
class MyClass: Object {
#objc dynamic var my_primary_id = NSUUID().uuidString
I am adding an additional answer that's somewhat related to the first but also stands on it's own.
In a nutshell, once Realm connects to a data source, it will continue to use that data source as long as the objects are not released, even if the actual file is deleted.
The way around that is to encapsulate the Realm calls into an autorelease pool so that those objects can be released when the Realm is deleted.
Here’s an example:
This function adds a GameData object to the default.realm file.
func addAnObject() {
autoreleasepool {
let realm = try! Realm()
let testData = GameData()
testData.Scenario = "This is my scenario"
testData.Id = 1
try! realm.write {
realm.add(testData)
}
}
}
At this point, if you run the addAnObject code, your file will have a GameData object.
GameData {
Id = 1;
GameDate = (null);
Scenario = This is my scenario;
GameStarted = 0;
}
Then a function that delete’s the old realm, and copies the bundled realm to it’s place. This works because all of the interaction with Realm was enclosed in an autorelease pool so the objects can be released.
func createDefaultRealm() {
let defaultURL = Realm.Configuration.defaultConfiguration.fileURL!
let defaultParentURL = defaultURL.deletingLastPathComponent()
if let bundledRealmURL = self.bundleURL("default") {
do {
try FileManager.default.removeItem(at: defaultURL)
try FileManager.default.copyItem(at: bundledRealmURL, to: defaultURL)
} catch let error as NSError {
print(error.localizedDescription)
return
}
}
let migrationBlock : MigrationBlock = { migration, oldSchemaVersion in
//handle migration
}
Realm.Configuration.defaultConfiguration = Realm.Configuration(schemaVersion: 18, migrationBlock: migrationBlock)
print("Your default realm objects: \(try! Realm().objects(GameData.self))")
}
func bundleURL(_ name: String) -> URL? {
return Bundle.main.url(forResource: name, withExtension: "realm")
}
and please note that if you access Realm inside the class but outside an autorelease pool, Realm will refuse to 'let go' of it's objects.
Do do NOT do this!!
class ViewController: UIViewController {
var realm = try! Realm()

iOS Swift Realm asyncOpen not working without internet

let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: realmURL))
Realm.asyncOpen(configuration: config) { realm, error in
if let realm = realm {
// Realm successfully opened, with all remote data available
}
else if let error = error {
// Handle error that occurred while opening or downloading the contents of the Realm
}
}
I used the above code to get Asynchronously Opening Realm, but its not working without internet.
As per realm Documents
In addition, synchronized Realms wait for all remote content available
at the time the operation began to be downloaded and available
locally.
syncConfiguration: will wait for all remote content available
For Example:
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: realmURL))
Realm.asyncOpen(configuration: config) { realm, error in
if let realm = realm {
// Realm successfully opened, with all remote data available
} else if let error = error {
// Handle error that occurred while opening or downloading the contents of the Realm
}
}

Changing IP address in Realm Object Server

I have an issue when the Realm Object Server have a different IP. The application can login through by Credential but after that it will return empty data although my database sit right on that IP and can be accessed by Realm Browser. Actually, I only use one account in realm object server and I create a user table with username and password so that after it can connect through Credential to the server, I will read the username and password on screen and check it information in database.
Connect to Realm Object Server function:
class func login(username: String, password: String, action: AuthenticationActions, completionHandler: #escaping ()->()) {
let serverURL = NSURL(string: realmIP)!
let credential = Credential.usernamePassword(username: username, password: password, actions: [action])
SyncUser.authenticate(with: credential, server: serverURL as URL) { user, error in
if let user = user {
syncUser = user
let syncServerURL = URL(string: realmURL)!
let config = Realm.Configuration(syncConfiguration: (user, syncServerURL))
realm = try! Realm(configuration: config)
} else if error != nil {
}
completionHandler()
}
}
Query from table after login by SyncUser:
class func loginLocal(employee: String) -> Bool{
let predicate = NSPredicate(format: "employee = %#", employee)
if (realm != nil) {
let user = realm?.objects(MyUser.self).filter(predicate)
if ((user?.count)! > 0) {
return true
}
}
return false
}
The solution seems to be weird so that I have to call a function multiple times by pressing my login button and hope that it will go through to the server.
This is my first application using Realm and Realm Object Server so I don't have much experience in this situation.
I may need more information on how you're handling the logged in Realm after the login, but from the code you've shown there, it looks like you're accidentally accessing a local version of the Realm and not the synchronized one.
Once logged in, you need to make sure you use the same Configuration object whenever you create Realm instances after that. It's not recommended to create and then save the realm instance inside the login completion block, as this block occurs on a background thread, making it unavailable anywhere else.
If your app is always online, it's easier to simply set the sync configuration as the default Realm for your app:
SyncUser.authenticate(with: credential, server: serverURL as URL) { user, error in
if let user = user {
syncUser = user
let syncServerURL = URL(string: realmURL)!
let config = Realm.Configuration(syncConfiguration: (user, syncServerURL))
Realm.Configuration.defaultConfiguration = config
}
completionHandler()
}
Otherwise, you can either save the Configuration in some sort of global object, or recreate it each time you need to create a Realm instance. The important thing to remember is you need to make sure your Realm instance is using a Configuration object with the successfully logged in user, otherwise it will default back to using a normal, empty local Realm.

Realm Swift Multi User Login

I am using Realm Swift in one of my iOS projects and one of the app requirements is to allow data of multiple users to co-exist. I am having an issue when the same users logs in as Realm is not able to identify the realm db file which is associated with that user.
Eg: So every time UserA re-logs in after logging out, a new Realm file is getting generated for UserA. This does not happen when UserA logs out and UserB logs in, then UserB logs out and UserA logs in.
UserA (logout) -> UserB (login) -> UseB (logout) -> UserA (login) [This works]
UserA (login) -> UserA (logout) -> UserA (login) [This does not work, a new Realm file is created and if there is a migration existing then try! Realm() also fails]
My AppDelegate code within application:didFinishLaunchingWithOptions looks like the following.
func setDefaultRealmForUser() {
var config = Realm.Configuration()
// Inside your application(application:didFinishLaunchingWithOptions:)
let currentLoggedInRegId = NSUserDefaults.standardUserDefaults().valueForKey(Constants.UserDefaults.CurrentLoggedInRegId)
if currentLoggedInRegId != nil {
let registrationId = currentLoggedInRegId as! String
// Use the default directory, but replace the filename with the username
config.fileURL = config.fileURL!.URLByDeletingLastPathComponent?
.URLByAppendingPathComponent("\(registrationId).realm")
}
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
}
and my loginViewController code on success looks like the following
func setDefaultRealmForUser(onComplete: ()->()) {
var config = Realm.Configuration()
let currentLoggedInRegId = NSUserDefaults.standardUserDefaults().valueForKey(Constants.UserDefaults.CurrentLoggedInRegId)
if currentLoggedInRegId != nil {
let registrationId = currentLoggedInRegId as! String
// Use the default directory, but replace the filename with the username
config.fileURL = config.fileURL!.URLByDeletingLastPathComponent?
.URLByAppendingPathComponent("\(registrationId).realm")
}
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
onComplete()
}
Update: I have made it work for the time being by loading the default Realm before loading the user realm config, which looks like the below code:
func reloadRealmWithDefault(onComplete: ()->()) -> (Void) {
var config = Realm.Configuration()
let defaultRealm = "default"
// Use the default directory, but replace the filename with the username
config.fileURL = config.fileURL!.URLByDeletingLastPathComponent?
.URLByAppendingPathComponent("\(defaultRealm).realm")
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
onComplete()
}
But i am not completely happy with this approach as it's more a hack job.
What's the best way of doing a multi user login scenario?
It's probably not the greatest to continually change which Realm file the default configuration is pointing to. Realm itself caches references to files internally for performance reasons, so its not recommended to change the configuration once the file has actually been opened.
For a multiple user managed system, it would definitely make sense to have one Realm file per user.
On the code architecture level, I think it would be appropriate to have a singleton object that manages the state of the current user, and provides appropriately formatted Configuration objects whenever you need them.
class User {
static let currentUser = User()
private var userID: String? = nil
public var configuration: Realm.Configuration {
let configuration = Realm.Configuration()
configuration.fileURL = URL(filePath: "\(userID).realm")
return configuration
}
public func logIn(withUserID userID: String) {
self.userID = userID
}
public func logOut() {
self.userID = nil
}
}
let userRealm = try! Realm(configuration: User.currentUser.configuration)

Resources