简体   繁体   English

我应该使用NSFetchedResultController还是Dictionary覆盖CoreData领域?

[英]Realm over CoreData should I use NSFetchedResultController or a Dictionary?

I'm working on an app using Core Data and NSFetchedResultController but for many reasons I would like to switch it to use Realm. 我正在使用Core Data和NSFetchedResultController开发一个应用程序,但是由于许多原因,我想将其切换为使用Realm。 I saw that there is a "Realm version" of the NSFetchedResultController on github but It wouldn't be compatible with my current code using Core Data. 我在github上看到了NSFetchedResultController的“领域版本”,但它与我当前使用Core Data的代码不兼容。

The view controller is displaying a list of People from the same school, like an address book. 视图控制器正在显示来自同一学校的人员列表,例如通讯录。 This list is a sublist of a people who studied in the same city. 此列表是在同一城市学习的人的子列表。

So I was thinking to make 1 unique request to the database to retrieve the list of all people and then filter locally this list within 1 Dictionary per school [String: AnyObject] with String as Section name within the tableView and AnyObject an array of people. 因此,我正在考虑向数据库发出1个唯一请求,以检索所有人员的列表,然后在每个学校的1个词典中对该列表进行本地过滤[String:AnyObject],其中tableView中的String为Section名称,AnyObject为人员数组。

  1. first solution, make a NSFetchedResultController for each school with a predicate filtering the location. 第一个解决方案是,为每个学校创建一个NSFetchedResultController,并用谓词过滤位置。 Then all delete actions etc.. are handled by the delegates -> this is not compatible with Realm 然后所有删除操作等都由委托人处理->与Realm不兼容

  2. Create those dictionaries and update them for each actions... -> this works with Realm but it's very annoying to code. 创建这些字典并为每个操作更新它们...->这与Realm一起使用,但是编写代码很烦人。

Any better solution? 有更好的解决方案吗?

EDIT: 编辑:

I need to clarify my request: I'd like to write a class that inherit UITableViewController. 我需要阐明我的要求:我想编写一个继承UITableViewController的类。

  • This class has a list of people sorted in the alphabetical order 本课程有按字母顺序排列的人员列表
  • The tableview has section with the first letter of their firstname tableview的部分带有其名字的首字母
  • The tableview controller needs to handle updates of the data model (insert, update, delete) tableview控制器需要处理数据模型的更新(插入,更新,删除)

As we might move from CoreData to Realm, I'd like to write code that is compatible with both so that I don't need to modify it again later. 当我们可能从CoreData转到Realm时,我想编写与两者兼容的代码,这样我以后就不需要再次修改它了。 The only "smart" way I found to do it so far is to forget about the NSFetchedResultController and the RBQFetchedResultsController, because they are respectively linked to CoreData and Realm, and then use data structure like Dictionaries. 到目前为止,我发现唯一做到这一点的“智能”方法就是忘记NSFetchedResultController和RBQFetchedResultsController,因为它们分别链接到CoreData和Realm,然后使用字典等数据结构。

Just to clarify, you're creating a UITableView with multiple sections; 为了澄清起见,您正在创建一个包含多个节的UITableView 1 per school, and you want to sort a flat list of people in a Realm database into the table based on their school, correct? 每个学校1个,并且您想根据他们的学校将Realm数据库中的人的简单列表排序到表中,对吗?

If that Realm fetched results controller you mentioned (I'm guessing it's RBQFetchedResultsController ) doesn't fit your app's architecture, then yeah, dictionaries would be the way to go, but it shouldn't hopefully be as 'manual' as you'd think. 如果您提到的Realm获取的结果控制器(我猜是RBQFetchedResultsController )不适合您应用的体系结构,那么是的,词典是您的理想之选,但不希望它像您所期望的那样“手动”认为。

The good thing about Realm Results objects are that they are 'live' in the sense that if a new item is added to Realm after the Results query was made, it'll be retroactively updated to include the new item. 关于Realm Results对象的好处是,它们是“活动的”,即如果在执行Results查询之后将新项目添加到Realm,则会追溯更新该项目以包括新项目。 That being the case, as long as you're managing a dictionary of Results objects that each relate to fetching the people for each specific school, the only manual aspect would be managing the table sections themselves. 在这种情况下,只要您管理一个Results对象字典,每个字典都与为每个特定学校招募人员有关,则唯一的手动方面就是管理表部分本身。

The only thing to be aware of is you'll need a mechanism to be notified when a new person has been added to a specific school (in order to know to refresh that section of the table view), but for now, it would be best if you did that in your own logic (Either through a callback block, or a delegate call). 唯一需要注意的是,当一个新的人被添加到特定学校时,您需要一种通知机制(以便知道刷新表格视图的该部分),但是就目前而言,这是最好是按照自己的逻辑(通过回调块或委托调用)进行。

On a sidenote, we're in the middle of adding a set of new APIs to make implementing native fetched results controller behaviour possible in Realm (ie, automating the need to post a notification when a new object is added). 在旁注中,我们正在添加一组新的API,以使在Realm中实现本机获取的结果控制器行为成为可能(即,在添加新对象时自动发布通知)。 We haven't got a proper release date confirmed yet, but it should be within the first quarter of 2016! 我们尚未确定适当的发布日期,但应该在2016年第一季度之内! :) :)

暂无
暂无

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

相关问题 我应该使用存储库for Realm(ios) - Should I use repository for Realm(ios) CoreData:使用NSFetchedResultController提取多个实体 - CoreData : Fetch Multiple Entities using NSFetchedResultController 用于单个UITable和CoreData Entity设计的多个NSFetchedResultController - Multiple NSFetchedResultController for a single UITable and CoreData Entity design 对于给定的配置,我应该尝试使用NSUserDefaults还是转到CoreData? - for the given configuration should I attempt to use NSUserDefaults or go to CoreData? 我应该为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? 我们可以免费使用 Realm 数据库来替代 Sqlite 和 CoreData 吗? - Can we use Realm database for free like alternative to Sqlite and CoreData? 什么时候应该在SKSpriteNode上使用SKShapeNode? - When should I use SKShapeNode over SKSpriteNode? 在保持NSFetchedResultController功能的同时保存单个CoreData实体(不是整个上下文) - Saving Single CoreData Entity (Not the Whole Context) While Keeping NSFetchedResultController Functionality 我可以将Realm用于所有数据存储,还是应该使用NSUserDefaults来存储用户名/密码? - Can I use Realm for all data storage, or should I use NSUserDefaults for storing username/password?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM