准备换数据库realm
环境xcode8.1 swift oc 混编 官方没有太多这样的说明 ,就是用oc 的库导入一个swift文件然后调用吧
文档页面:
https://realm.io/docs/objc/latest/
####1 创建数据库文件,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| func createRleam(dataName:String){ let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first if FileManager.default.fileExists(atPath: "\(path!)/realm") == false{ try! FileManager.default.createDirectory(atPath: "\(path!)/realm", withIntermediateDirectories: true, attributes: nil) } let filePath = path! + "/realm/\(dataName)" let config = RLMRealmConfiguration.default() config.fileURL = URL(string: filePath) config.readOnly = false let currentVersion:UInt64 = 1 config.schemaVersion = currentVersion config.migrationBlock = { (migration,oldSchemaVersion) in if oldSchemaVersion < currentVersion{ } } RLMRealmConfiguration.setDefault(config) let folderPath = RLMRealm.default().configuration.fileURL?.deletingLastPathComponent().path Getdevice.println("父级路径\(folderPath)") if let folderPath = folderPath{ try? FileManager.default.setAttributes([FileAttributeKey.protectionKey:FileProtectionType.none], ofItemAtPath: folderPath) } }
|
####2 创建 一个数据模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class RLMHome: RLMObject { override init() { super.init() } override init(value: Any) { super.init(value: value) } dynamic var index = "" dynamic var homedata:Data? override class func primaryKey()->String{ return "index" } override class func requiredProperties()->Array<String>{ return ["index"] } override class func indexedProperties()->Array<String>{ return ["index"] } }
|
####3 数据的增 或者更新
1 2 3 4 5 6 7 8 9 10
| let home = RLMHome() home.index = "0" let data = try? json.rawData() home.homedata = data let realm = RLMRealm.default() realm.beginWriteTransaction() RLMHome.createOrUpdate(in: realm,withValue: home) try? realm.commitWriteTransaction()
|
####4 数据的查询
1 2 3 4 5 6 7 8 9 10
| //查询所有 这个数据查询是没有分页的,因为懒加载,用得时候在读取,所以分页自己分楼 let homes = RLMHome.allObjects() if homes.count>0 && (homes[0] as! RLMHome).homedata != nil{ let data = (homes[0] as! RLMHome).homedata let json = JSON(data: data!) if json["code"].intValue == CompleteCode{ complete(json) return }
|
查询遇到的坑, “id == 123” ,id为主键, 查询失败 得id == ‘123’ , 在另外一个地方非主键查询得不嫁单引号,
数据更新和结构问题
1
| self.downDataCompleteArray = RLMDownloads.objects(where: "downStatus == 1") as! RLMResults<RLMDownloads>
|
结构 RLMResults<自定义>
类似数组但是不是数组, 如果用他来更新ui 或者说realm数据库更新ui 都得用他们的消息机制,接收到更改刷新ui
比如我的
1 2 3 4 5
| self.RLMNotificationTokenReadun = self.downDataUnderwayArray.addNotificationBlock { [weak self] (results:RLMResults<RLMDownloads>?, change:RLMCollectionChange? , error) in if change != nil{ self?.underwayTableView.reloadData() } }
|
deinit 的时候self.RLMNotificationTokenReadun.stop()一下
可以说方便了,但是和原有项目数据结构相差还是比较大得
遇到的小问题 1:
数据更新问题, 开始整体更新
1 2 3 4 5 6 7 8 9 10 11 12
| home = RLMHome() home.index = "0" home.homedata = data DispatchQueue(label: "rleam").async { let realm = RLMRealm.default() realm.beginWriteTransaction() RLMHome.createOrUpdate(in: realm,withValue: home) try? realm.commitWriteTransaction() } }
|
没问题 ,后来准备局部更新 , 发现这样方式失败 , 就算查询出来更改同样崩溃 后来发现api 上有个更新
let realm = RLMRealm.default()
try? realm.transaction{ () in
home.homedata = data
}