我正在使用Realm它看起来不错 . 但我在这里很困惑 . 因为我没有了解Realm如何工作以及它保存数据的结构是什么 .
我基本上是Android开发人员,你可以说我是iOS新手 . 所以在我看来,我在想,会有一个数据库的主文件 . 然后在里面,会有不同的表格和表格,我可以保存数据 .
但我很惊讶我保存了数据,但它没有问我表名,我真的不知道如何在其中创建表 . 在Java中我们首先创建表,然后我们将模型反映为数据库中该表中的行 . 我正在使用以下文件来保存Realm中的数据 . 请看一下并清楚以下混淆
告诉我领域是如何运作的?
如何在域中创建表?
如何检查db是否存在我的意思是数据是否已保存?
如何检查是否有任何表说(国家表)已经创建,以便检索数据?
并看到以下课程 . 它基本上是一个帮助类 .
public class DbHelper {
private init() {}
static let sharedDbHelper = DbHelper()
var realmObj = try! Realm()
/**
Generic function to create Object in the DB
*/
func save (_ obj : T){
do {
try realmObj.write {
realmObj.add(obj)
}
}catch{
print("DbHelperException","Create",error)
}
}
/**
Generic function to update Object in the DB
*/
func update (_ obj : T, with dictionary: [String : Any?]){
do{
try realmObj.write {
for (key,value) in dictionary{
obj.setValue(value, forKey: key)
}
}
}catch {
print("DbHelperException","Update",error)
}
}
/**
Generic function to delete Object in the DB
*/
func delete (_ obj : T){
do {
try realmObj.write {
realmObj.delete(obj)
}
}catch {
print("DbHelperException","Delete",error)
}
}
/**
Function to manage the error and post it
*/
func postDbError(_ error : Error) {
NotificationCenter.default.post(name: NSNotification.Name(""), object: error)
}
/**
Function to observe the error and post it
*/
func observeDbErrors(in Vc: UIViewController, completion: @escaping (Error?) -> Void) {
NotificationCenter.default.addObserver(forName: NSNotification.Name(""), object: nil, queue: nil) { (notification) in
completion(notification.object as? Error)
}
}
/**
Function to remove observer of the error
*/
func stopDbErrorObserver (in Vc: UIViewController ){
NotificationCenter.default.removeObserver(Vc, name: Notification.Name(""), object: nil)
}
}