ios - Adding Objects to single Realm File -


before begin here bit background possible answers won't go beyond comprehension level: 1. i'm jolt starting learn swift , still learning in , outs of both realm , xcode. 2. oop experience has been java, , @ low.

so here issue:

i'm trying make single realm file hold entire list of "user profile" data(ie. name, age, email). i'm attempting allowing ibaction button cause object saved realm file shown below

@ibaction func signupbutton(_ sender: uibutton) {      let realm = try! realm()        try! realm.write {      user.username = usernametextfield.text!     user.password = passwordtextfield.text!     user.email = emailtextfield.text!     user.name = fullnametextfield.text!     user.age = agetextfield.text!      profile.person.append(user)       realm.add(profile)      } } 

the problem here it's not adding object instead updating 1 created before, can tell me how can accomplish using ibaction button?

if object has been added realm file, can change properties opening write transaction , modifying properties in there.

let realm = try! realm()  let newuser = user() newuser.username = usernametextfield.text!  // add realm first time try! realm.write {    realm.add(newuser) }  // update property @ later time try! realm.write {    newuser.username = usernametextfield.text! } 

it's not necessary call realm.add or profile.person.append(user) again if objects added.

i'm not sure user , profile coming in example code there. since there's no let user = user() inside method, i'm assuming you're creating single copies of them elsewhere in view controller class.

if profile has been added realm, shouldn't calling realm.add(profile) again, add second copy (and calling append each time probably won't break, it's not recommended).

to work out if profile inside realm file, can check using profile.realm != nil. check if user belongs in profile, can user realm's inverse relationships feature.

class user: object {    dynamic var username = ""    dynamic var password = ""    dynamic var email = ""    dynamic var name = ""    dynamic var age = ""     let profile = linkingobjects(fromtype: profile.self, property: "person") } 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -