First of all I'm relatively new in ios and have no any experience using mobile dbs.
Wanted to integrate into my app Realm(swift) and wonder if it make sense to have service layer and repository separated or everything just include into the service.
Some example to have a good view.
class UserService {
var userRepository: UserRepository!
func findById(userId: String) -> User? {
return userRepository.findById(userId: userId)
}
}
class UserRepository {
private let realm = try! Realm()
func findById(userId: String) -> User? {
return realm.object(ofType: User.self, forPrimaryKey: userId)
}
}
Realm is a cross-platform mobile database solution designed for mobile applications that you can integrate with your iOS projects. Unlike wrappers around Core Data, Realm doesn't rely on Core Data or even an SQLite back end. This tutorial introduces you to the basic features of Realm on iOS.
Realm objects have bindings to SwiftUI controls and, much like toggling the bought property, they already start a realm transaction to write the changes in the database whenever you change those values.
Adding an abstraction layer over the top of database APIs is pretty common. A lot of other developers have wrapped Realm in their own classes in order to hide the API from their business logic code.
There's a couple of considerations in which to be aware:
As long as the new database you'd be moving to consists of managing objects as well (ie Core Data), converting from one database to another isn't usually a lot of work. As such, I'd recommend avoiding unnecessary work until the time when it becomes necessary.
Disclaimer: I work for Realm, but this is my opinion as someone who's shipped personal apps using Core Data, raw SQLite and Realm in the past.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With