0

I have an issue with realm for my iOS app. I previously used Firestore, which worked fine, but it became troublesome after using it. I'be incorporated Realm and I'm trying to get the user's details through a series of "Register" View controllers. Basically, I'm collecting their data via each view controller.

I commit at the the start of the registration(when they have been authenticated through google or apple sign in), which commits their email, first name,last name(through google or apple login) and their location. Then through of series of view controllers I collect more data to be stored onto the same row in realm db. The data added to the AuthenticationViewController after gets posted on an unrelated row or not at all. The data below (realmUser.firstname etc) gets posted in a row exactly as I intended. I know I shouldn't be committing from the start but I'm unsure how to work around it.

        realmUser.firstName = firstName
        realmUser.secondName = secondName
        realmUser.id = emailId
        realmUser.longitude = userlocation.longitude
        realmUser.latitude = userlocation.latitude
        
        do {//commit changes to realm db
            try realm.write{
                realm.add(realmUser)
            }
        } catch {
            print("Error \(error)")
        }
cngzz1
  • 143
  • 2
  • 9
John Doe
  • 79
  • 1
  • 4
  • For clarity;Realm does not have tables or rows, just objects. If your first viewController adds the object to realm, it becomes a *managed object*. You can add more data to that object but any modifications must be done within a write. It would probably be better to create the object, populate it and then at the end write it to realm. Other than that, the code in your question works so perhaps you can clarify what the actual issue is? Do you have a piece of code that isn't working? Please review [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay Dec 26 '20 at 15:27
  • Thanks for your reply. Yes I am aware it is an object, I just used rows for an analogy. and because when I view the actual realm file, like you said I need to write only when in finished editing, but I'm struggling to collect the data when going through a series of view controllers. – John Doe Dec 27 '20 at 16:26
  • That doesn't sound really like a Realm issue or question but more how to work with viewControllers. I would suggest perhaps passing object via a segue from one to the other or maybe having a master viewController that contains the object that each of the child viewControllers can access. See the answers to this question [passing objects between view controllers](https://stackoverflow.com/questions/25215476/how-do-you-pass-data-between-view-controllers-in-swift) – Jay Dec 27 '20 at 18:24

0 Answers0