【问题标题】:Saving different categories with Realm, Swift使用 Realm、Swift 保存不同的类别
【发布时间】:2020-11-22 10:17:12
【问题描述】:

我正在做一个金融应用程序。我正在使用领域保存数据,但我无法理解一件事。用户输入后,他花了多少钱,他可以选择一个类别:超市,运输等。所以问题是我如何按类别保存用户的数据。领域模型:

class Items: Object {
    @objc dynamic var personID = UUID().uuidString
    @objc dynamic var categoryAmount = ""
    @objc dynamic var categoryName = ""

    override static func primaryKey() -> String? {
        return "personID"
    }  
}
class RealmModel {
    static let shared = RealmModel()
    private let realm = try! Realm()
    let myPrimaryKey = "Primary-Key"

    func addAmount(newItem: String) {
        let categoryAmount = realm.object(ofType: Items.self, forPrimaryKey: myPrimaryKey)
        let new = Items()
        new.personID = "My-Primary-Key"
        new.categoryAmount = newItem
    
        try! realm.write {
            realm.add(new, update: .modified)
        }
    }
    
    func updateAmount(editedItem: Items, newItem: String) {
        try! realm.write {
            editedItem.categoryAmount = newItem
        }
    }
    
    func getAmount() -> Items? {
            let amount = realm.objects(Items.self).last
            return amount
        }
    
    func setAmount(newItem: String) {
        if let data = getAmount() {
            updateAmount(editedItem: data, newItem: newItem)
        } else {
            addAmount(newItem: newItem)
        }
    }
}

按钮:

@IBAction func supermarketBTN(_ sender: Any) {
    RealmModel.shared.setAmount(newItem: "\((Double(exileAmount?.categoryAmount ?? "" ) ?? 0) + (Double(self.amountTF.text ?? "") ?? 0))")
    self.exileAmount = RealmModel.shared.getAmount()
    delegate?.updateAmount(amount: self.amountTF.text ?? "")
    dismiss(animated: true, completion: nil)
    
    }

我应该如何使用 categoryName 来按类别保存数据?

【问题讨论】:

    标签: ios swift realm realm-mobile-platform


    【解决方案1】:

    有 100 种不同的方法来解决这个问题。让我从高层次上介绍一种可能性。

    第一个问题是改变对象的结构。你需要两个;一个存储用户,另一个存储他们的类别选择和花费的金额

    class UserClass: Object {
       @objc dynamic var userId = "" //however you want to define id's
       @objc dynamic var userName = ""
    }
    

    然后

    class SpentMoneyClass: Object {
       @objc dynamic var _id = UUID().uuidString
       @objc dynamic var category = ""
       @objc dynamic var byUserId = ""
       @objc dynamic var amount = 0.0
    }
    

    您的数据库将包含用户,并且每个用户对象都有一个 userId。当用户花钱时,创建一个新的 SpentMoneyClass 并填写属性 - 假设用户 Artim 在 Travel 上花钱

    let spend = SpentMoneyClass()
    spend.category = "Travel"
    spend.byUserId = artimUser.userId
    spend.amount = 100.00
    

    将对象写入领域

    此结构使您可以查询 Artim 花费的所有金额、所有用户花费的所有旅行费用,以及更精细地查询 Artim 花费的所有旅行费用或特定金额 - 您可以利用聚合函数 .sum 来获取总计非常简单:

    let travelTotal: Double = realm.object(SpentMoneyClass.self)
                                   .filter("byUserId == x and category == 'Travel'")
                                   .sum(ofProperty: "amount")
    

    给出 Artim 在旅行上的总花费(x = Artims 用户 ID)

    *注意我省略了 MongoDB Realm 所需的主键和 _id 属性以保持长度较短

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多