Created
February 1, 2024 07:50
-
-
Save Innovatewithapple/f5b1a751619657ef461f294a93bb7576 to your computer and use it in GitHub Desktop.
RealmDB Snippt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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