Each Device get only the own record, Syncing does not work (for me)

cloud

#1

Hi,

i am new here, this is my first Question.
I am working with Swift.

I have a realm cloud, and i will save for each user a “PlayerActivity” record. it works, but each device finds only the own record (saved from the device), all others (saved from other devices) are not founded.

I work with the next code:

private func loginToRealmSync(new: Bool, userName: String) {
    let password = “xxx” + userName + “yyy”
    let logInCredentials = SyncCredentials.usernamePassword(username: userName, password: password)
    SyncUser.logIn(with: logInCredentials, server: GV.AUTH_URL) { user, error in
        if user == nil {  // create a new Account
            let signUpCredentials = SyncCredentials.usernamePassword(username: userName, password: password, register: true)
            SyncUser.logIn(with: signUpCredentials, server: GV.AUTH_URL) { user, error in
                if user == nil {
                    print("Error, user couldn't be created")
                } else {
                    let logInCredentials = SyncCredentials.usernamePassword(username: userName, password: password)
                    SyncUser.logIn(with: logInCredentials, server: GV.AUTH_URL) { user, error in
                        if user == nil {
                            print("error after register")
                        } else {
                            GV.myUser = user
                            realm.beginWrite()
                            GV.basicDataRecord.myName = userName
                            //                print(textField.text)
                            try! realm.commitWrite()
                            self.setIsOnline()
                            print("OK after register")
                        }
                    }
                }
            }
        } else {
            print("OK user exists")
            GV.myUser = user
            self.setIsOnline()
        }
    }
}


func setIsOnline() {
    let syncConfig: SyncConfiguration = SyncConfiguration(user: GV.myUser!, realmURL: GV.REALM_URL)
    let config = Realm.Configuration(syncConfiguration: syncConfig, objectTypes: [BestScoreSync.self, PlayerActivity.self])
    realmSync = try! Realm(configuration: config)

    if playerActivity == nil {
        playerActivity = realmSync?.objects(PlayerActivity.self).filter("name = %@", GV.basicDataRecord.myName)
    }
    try! realmSync?.write {
        if playerActivity?.count == 0 {
            let playerActivityItem = PlayerActivity()
            playerActivityItem.name = GV.basicDataRecord.myName
            playerActivityItem.nickName = GV.basicDataRecord.myNickname
            playerActivityItem.isOnline = true
            playerActivityItem.onlineSince = Date()
            playerActivityItem.onlineTime = 0
            realmSync?.add(playerActivityItem)
        } else {
            playerActivity![0].isOnline = true
            playerActivity![0].onlineSince = Date()
        }
    }
    setNotification()
}

import Foundation
import RealmSwift

class PlayerActivity: Object {

// Specify properties to ignore (Realm won't persist these)
@objc dynamic var name: String = UUID().uuidString
@objc dynamic var nickName: String?
@objc dynamic var isOnline: Bool = false
@objc dynamic var onlineSince: Date?
@objc dynamic var onlineTime: Int = 0

override static func primaryKey() -> String? {
    return "name"
}

}

In the viewController:

override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
view.addSubview(tableView)
tableView.frame = self.view.frame
subscription = playerActivityItems.subscribe(named: “myUserActivitys”)
subscriptionToken = subscription.observe(.state) { [weak self] state in
print(“in Subscription!”)
switch state {
case .creating:
print(“creating”)
// The subscription has not yet been written to the Realm
case .pending:
print(“pending”)
// The subscription has been written to the Realm and is waiting
// to be processed by the server
case .complete:
print(“complete: count records: (String(describing: self!.playerActivityItems.count))”)
// The subscription has been processed by the server and all objects
// matching the query are in the local Realm
case .invalidated:
print(“invalitdated”)
// The subscription has been removed
case .error(let error):
print(“error: (error)”)
// An error occurred while processing the subscription
}
}
notificationToken = playerActivityItems.observe { [weak self] (changes) in
guard let tableView = self?.tableView else { return }
switch changes {
case .initial:
// Results are now populated and can be accessed without blocking the UI
tableView.reloadData()
case .update(_, let deletions, let insertions, let modifications):
// Query results have changed, so apply them to the UITableView
tableView.beginUpdates()
tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
with: .automatic)
tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
tableView.endUpdates()
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError("(error)")
}
}
}

In Realm Studio i see only one record.

The Realm Users are NOT Admins!

I have tested the ToDoAppPartial from Realm Cloud documentation, it has worked for me.
But: in the sample is the SyncCredentials.nickname used, that is not recommended for Releases.

Could anybody help me?


#2

Do the users have permission to at least read the other’s realm?

Currently, you are writing the users data their own realm, not a shared realm.


#3

Thank You!
I have created a user in Realm Studio with admin privileges and all devices make login with the same user. Now it works perfect!

Jozsef :wink: