ios swift java_在iOS swift中使用Realm 4

我正在使用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)

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值