Realm二次封装实现增删查改

2017-05-29  本文已影响454人  jackyshan
image

Realm 是 SQLite 和 Core Data 的替代者,得益于其零拷贝的设计,Realm 比任何 ORM 都要快很多。几分钟内就能学会使用 Realm。

封装了基础类DBBaseObject,基础类实现了增删查改的方法,所有数据Object都继承DBBaseObject
新建Dog类,继承DBBaseObject

class Dog: DBBaseObject {
    dynamic var name = ""
    dynamic var age = 0
}

let dog = Dog()
dog.name = "xiaogou"
dog.add()

let dogs = Dog.query(Dog.self, "name = 'xiaogou'")
dogs.count

if let dog = dogs.first {
    Dog.update {
        dog.name = "dagou"
    }
}

if let dog = dogs.first {
    dog.delete()
}

DBBaseObject实现

//
//  DBBaseObject.swift
//  DianZiCheng
//
//  Created by jackyshan on 2017/5/28.
//  Copyright © 2017年 jackyshan. All rights reserved.
//

import UIKit

class DBBaseObject: Object {
    dynamic var note1: String?//备份
    dynamic var note2: String?//备份
    
    func add() {//增
        let realm = try! Realm()
        try! realm.write {
            realm.add(self)
        }
    }
    
    func delete() {//删
        let realm = try! Realm()
        try! realm.write {
            realm.delete(self)
        }
    }
    
    static func query<T: Object>(_ type: T.Type, _ filterStr: String? = nil) -> Results<T> {//查
        let realm = try! Realm()
        
        if let str = filterStr {
            return realm.objects(type).filter(str)
        }
        else {
            return realm.objects(type)
        }
    }
    
    //多线程查询
    static func query<T: Object>(_ type: T.Type, _ filterStr: String? = nil, block:@escaping ((_ results: Results<T>)->Void)) {
        DispatchQueue(label: "background").async {
            let realm = try! Realm()
            
            if let str = filterStr {
                let objects = realm.objects(type).filter(str)
                block(objects)
            }
            else {
                let objects = realm.objects(type)
                block(objects)
            }
        }
    }
    
    static func update(_ block: (() throws -> Swift.Void)) {//改
        let realm = try! Realm()
        try! realm.write(block)
    }
}
上一篇下一篇

猜你喜欢

热点阅读