简体   繁体   English

将对象添加到单个Realm文件

[英]Adding Objects to single Realm File

Before I begin here is a bit a background for you all so your possible answers won't go beyond my comprehension level: 1. I'm only jolt starting to learn swift and is still learning the in and outs of both realm and Xcode. 在我开始之前,这里为您提供了一些背景知识,因此您可能的答案将不会超出我的理解水平:1.我只是开始学习快速而感到震惊,并且仍在学习领域和Xcode的来龙去脉。 2. My only OOP experience has been with java, and at that very low. 2.我唯一的OOP经验是使用Java,当时还很低。

So here is my issue: 所以这是我的问题:

I'm trying to make a single realm file hold an entire list of "user profile" data(ie. name, age, email). 我正在尝试使单个领域文件包含“用户个人资料”数据(即名称,年龄,电子邮件)的完整列表。 I'm attempting to do this by allowing an IBAction button to cause an object to be saved to the realm file as shown below 我正在尝试通过允许IBAction按钮将对象保存到领域文件中来执行此操作,如下所示

@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 only problem here is that it's not adding object but instead updating the one that was created before, can anyone tell me how I can accomplish this using an IBAction Button? 唯一的问题是,它不是在添加对象而是在更新以前创建的对象,有人可以告诉我如何使用IBAction按钮完成此操作吗?

If your object have primary key, you can only add new by fetch then delete it, then add new one, else you can only update it (primary key is used to prevent duplication) 如果您的对象具有主键,则只能通过提取来添加新对象,然后将其删除,然后再添加新对象,否则只能对其进行更新(主键用于防止重复)

If you want to have 2 object that have similar property value, then just simply remove the primary key from the object class 如果要具有2个具有相似属性值的对象,则只需从对象类中删除主键

If an Object has already been added to a Realm file, then you can change its properties by opening up a write transaction and simply modifying its properties in there. 如果Object已经添加到Realm文件中,则可以通过打开写事务并在其中简单地修改其属性来更改其属性。

let realm = try! Realm()

let newUser = User()
newUser.userName = userNameTextField.text!

// Add to Realm for the first time
try! realm.write {
   realm.add(newUser)
}

// Update its property at a later time
try! realm.write {
   newUser.userName = userNameTextField.text!
}

It's not necessary to call realm.add or profile.person.append(user) again if those objects were already previously added. 如果先前已添加这些对象,则不必再次调用realm.addprofile.person.append(user)

I'm not sure where user and profile are coming from in your example code there. 我不确定您的示例代码中userprofile的来源。 Since there's no let user = User() inside that method, I'm assuming you're creating single copies of them elsewhere in the view controller class. 由于该方法中没有let user = User() ,因此我假设您在视图控制器类中的其他位置创建它们的单个副本。

If profile has already been added to the Realm, you shouldn't be calling realm.add(profile) again, as that will add a second copy (And calling append each time probably won't break, but it's not recommended). 如果profile已经被添加到域,你不应该调用realm.add(profile)再次,因为这将增加第二个拷贝(并呼吁append 大概每次不会打破,但它不推荐)。

To work out if profile is already inside a Realm file, you can check by using profile.realm != nil . 要确定profile是否已经在Realm文件中,可以使用profile.realm != nil进行检查。 To check if user already belongs in profile , you can user Realm's inverse relationships feature. 要检查user已经属于profile ,可以使用Realm的逆向关系功能。

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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM