Skip to content

Instantly share code, notes, and snippets.

@Innovatewithapple
Created February 1, 2024 07:50
Show Gist options
  • Save Innovatewithapple/f5b1a751619657ef461f294a93bb7576 to your computer and use it in GitHub Desktop.
Save Innovatewithapple/f5b1a751619657ef461f294a93bb7576 to your computer and use it in GitHub Desktop.
RealmDB Snippt
class DatabaseHelper {
static let shared = DatabaseHelper()
private var realm = try! Realm()
private init(){}
func getDatabaseURL() -> URL? {
return Realm.Configuration.defaultConfiguration.fileURL
}
func saveContact(contact:Contact) {
try! realm.write {
realm.add(contact)
}
}
func deleteContact(contact:Contact){
try! realm.write{
realm.delete(contact)
}
}
func updateContact(oldContact:Contact, newContact:Contact){
try! realm.write{
oldContact.firstName = newContact.firstName
oldContact.lastName = newContact.lastName
}
}
func getAllContact() -> [Contact] {
return Array(realm.objects(Contact.self))
}
}
class Contact:Object{
@Persisted var firstName:String
@Persisted var lastName:String
convenience init(firstName: String, lastName: String) {
self.init()
self.firstName = firstName
self.lastName = lastName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment