Swift - 使用Realm (2) 簡易CRUD

本篇摘要

  • Realm CRUD
  • Realm 參考好文





  • 建立簡單的View



  • 新增一個Realm data Model 取名為RealmItem
class RealmItem: Object {
    @objc dynamic var id = 0
    @objc dynamic var textString = ""
    
    override static func primaryKey() -> String? {
        return "id"
    }
}
  • 新增RealmModel
class RealmModel{
    
    var id : Int?
    var textString : String?
}

  • 建立RealmAdapter 負責存取Realm的資料
class RealmItemAdapter {
    private var database:Realm
    static let sharedInstance = RealmItemAdapter()
    
    private init() {
        database = try! Realm()
        
        
        print(Realm.Configuration.defaultConfiguration.fileURL!)
    }
    
    //get all record
    func getItem() -> [RealmModel] {
        
        var realmModels : [RealmModel] = []
        let results: Results = database.objects(RealmItem.self)

        if results.count > 0 {
            
            results.forEach { item in
                
                let model = RealmModel()
                model.id = item.id
                model.textString = item.textString
                realmModels.append(model)
            }
        }
        
        return realmModels
    }
    
    //insert record
    func addItem(model: RealmModel)   {
        try! database.write {
            
            let item = RealmItem()
            item.id = incrementID()
            item.textString = model.textString!
            
            database.add(item)
        }
    }
    
    //update record by id
    func updateByID(model: RealmModel)   {
        if let item = database.objects(RealmItem.self).filter("id == %@", model.id!).first{
            try! database.write {
                
                item.id = model.id!
                item.textString = model.textString!

                database.add(item, update: true)
            }
        }
    }
    
    //delete all
    func deleteAllFromDatabase()  {
        try! database.write {
            database.deleteAll()
        }
    }
    
    //delete by id
    func deleteByID(id: Int)   {
        
        let itemDelete = database.objects(RealmItem.self).filter("id == %@", id)
        
        try!   database.write {
            database.delete(itemDelete)
        }
    }
    
    private func incrementID() -> Int {
        return (database.objects(RealmItem.self).max(ofProperty: "id") as Int? ?? 0) + 1
    }
}

  • 在controller為CRUD Button新增事件
   @IBAction func createClick(_ sender: UIButton) {
        
        let model = RealmModel()
        model.textString = lbInput.text
        RealmItemAdapter.sharedInstance.addItem(model: model)
        
    }
    
    @IBAction func readClick(_ sender: UIButton) {
        
        if let model = RealmItemAdapter.sharedInstance.getItem().first
        {
            currentModel = model
            lbOutput.text = model.textString
        }
    }
    
    @IBAction func updateClick(_ sender: UIButton) {
        
        if let model = currentModel
        {
            model.textString = lbInput.text
            RealmItemAdapter.sharedInstance.updateByID(model: model)
        }
    }
    
    @IBAction func deleteClick(_ sender: UIButton) {
        RealmItemAdapter.sharedInstance.deleteAllFromDatabase()
    }
 








  • 參考好文:
  • https://medium.com/@riccione83/easy-use-of-realm-in-swift-444f41a5742d
  • https://ithelp.ithome.com.tw/articles/10183329
  • 留言

    這個網誌中的熱門文章

    Swift - 自訂navigation bar style