简体   繁体   English

我应该使用存储库for Realm(ios)

[英]Should I use repository for Realm(ios)

First of all I'm relatively new in ios and have no any experience using mobile dbs. 首先,我在ios中相对较新,没有任何使用移动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. 想要集成到我的应用程序Realm(swift),并想知道将服务层和存储库分开或者所有内容都包含在服务中是否有意义。

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)
    }
}

Adding an abstraction layer over the top of database APIs is pretty common. 在数据库API顶部添加抽象层非常常见。 A lot of other developers have wrapped Realm in their own classes in order to hide the API from their business logic code. 许多其他开发人员已经将Realm包装在他们自己的类中,以便将API隐藏在他们的业务逻辑代码中。

There's a couple of considerations in which to be aware: 需要注意几个方面的考虑因素:

  1. You need to be carful not to accidentally hurt performance from this. 你需要保持小心,不要意外地伤害这种表现。 Some users have gone so far as to copy data out of Realm objects into their own objects. 一些用户甚至将数据从Realm对象复制到他们自己的对象中。 This defeats the purpose of Realm's 'zero-copy' mechanism, and so the app now inherently performs worse than using Realm natively. 这违背了Realm的“零拷贝”机制的目的,所以应用程序现在本身比本机使用Realm更糟糕。
  2. This is 'pre-emptive work'. 这是“先发制人的工作”。 You're doing a lot of work up front, just in case you might change your mind down the line. 你事先做了很多工作, 以防万一你可能会改变主意。 I converted Core Data to Realm in a rather large app a while ago, and it only took a few hours. 我刚刚在一个相当大的应用程序中将Core Data转换为Realm,它只花了几个小时。 Trying to architect a 'generic' database solution which you may never end up using sounds like it might not pay off. 试图构建一个“通用”数据库解决方案,您可能永远不会最终使用它可能无法获得回报的声音。
  3. You're increasing app complexity. 您正在增加应用复杂性。 This means more potential for bugs and more rigorous testing would be needed to make sure your API and the database API are in sync. 这意味着更多的漏洞可能需要更严格的测试,以确保您的API和数据库API保持同步。

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. 免责声明:我为Realm工作,但这是我的观点,因为过去曾使用Core Data,原始SQLite和Realm发布个人应用程序。

You can use extensions to add your fetching methods. 您可以使用扩展来添加提取方法。 You add Object subclasses for each entity in your database and then add extensions to each for these methods when needed. 您可以为数据库中的每个实体添加Object子类,然后在需要时为这些方法添加扩展。 Example: 例:

import RealmSwift

// Dog model
class Dog: Object {
    dynamic var name = ""
    dynamic var owner: Person? // Properties can be optional
}

The for your fetching methods: 对于你的提取方法:

extension Dog {
    class func fetch(with name: String) -> Dog? {
        return try! Realm().objects(Dog.self).filter("name == %@", name).first
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该在iOS上的Realm中删除旧表吗? - Should I delete old tables from Realm on iOS? 我应该使用NSFetchedResultController还是Dictionary覆盖CoreData领域? - Realm over CoreData should I use NSFetchedResultController or a Dictionary? 我应该为Realm.write()使用[unown self]吗? - Should l I use [unowned self] for Realm.write()? 我应该在混合的Objective-C / Swift项目中使用Realm Objective-C或Realm Swift吗? - Should I use Realm Objective-C or Realm Swift in my mixed Objective-C / Swift project? iOS 使用realm做缓存效率高吗? - iOS Is it efficient to use realm for cache? 在iOS中,我是否应该将文件扩展名为.a的文件提交到我的Merurial存储库? - In iOS, should I be committing files with a `.a` file extension to my mercurial repository? 我应该在iOS应用程序的git存储库中忽略哪些文件/文件夹? - What files/folders should I ignore in a git repository of an iOS app? 您是否应该在Realm iOS中异步存储图像? - Should you asynchronously store images in Realm iOS? 我可以将Realm用于所有数据存储,还是应该使用NSUserDefaults来存储用户名/密码? - Can I use Realm for all data storage, or should I use NSUserDefaults for storing username/password? iOS应用程序:什么时候应该清除缓存数据? (使用领域) - iOS app: When should I clear my cache data? (using Realm)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM