本文介绍了对核心数据数据结果进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 iPhone 上对一个想法进行原型设计,但我正处于 SQLite 与 CoreData 的十字路口.主要原因是我似乎无法弄清楚如何对核心数据进行分组.

I am prototyping an idea on the iPhone but I am at the SQLite vs CoreData crossroads. The main reason is that I can't seem to figure out how to do grouping with core data.

基本上我想显示按用户名分组的最新发布的项目.在 SQL 语句中确实很容易做到,但我无法使其在核心数据中工作.我想既然我正在启动一个新应用程序,我不妨尝试让核心数据工作,但这部分是一个主要障碍.

Essentially I want to show the most recent item posted grouped by username. It is really easy to do in a SQL statement but I have not been able to make it work in core data. I figure since I am starting a new app, I might as well try to make core data work but this part is a major snag.

我在我的 fetchrequest 中添加了一个谓词,但它只给了我最近添加的单个记录,而不是每个用户最近添加的记录.

I added a predicate to my fetchrequest but that only gave me the single most recently added record and not the most recently added record per user.

此时数据模型非常基本.它使用以下字段:用户名(字符串),帖子(字符串),创建(日期时间)

The data model is pretty basic at this point. It uses the following fields: username (string), post (string), created (datetime)

长话短说,CoreData 可以进行这些类型的查询吗?我想如果 SQLite 在幕后,一定有办法做到这一点.

So long story short, are these types of queries possible with CoreData? I imagine that if SQLite is under the hood, there has to be some way to do it.

推荐答案

首先,不要将 Core Data 视为执行 SQL 的另一种方式.SQL 不是 Core Data 的幕后".核心数据处理对象.实体描述不是表格,实体实例不是记录.使用 Core Data 编程与 SQL 无关,它只是将 SQL 作为几种可能的持久存储类型之一.您不会直接处理它,也不应该在 SQL 术语中考虑 Core Data.

First of all, don't think of Core Data as another way of doing SQL. SQL is not "under the hood" of Core Data. Core Data deals with objects. Entity descriptions are not tables and entity instances are not records. Programming with Core Data has nothing to do with SQL, it merely uses SQL as one of several possible types of persistent stores. You don't deal with it directly and should never, ever think of Core Data in SQL terms.

那种方式就是疯狂.

你需要喝大量的龙舌兰酒并反复打自己的脑袋,直到你忘记了你所知道的关于 SQL 的一切.否则,您最终会得到一个对象图,它只不过是一个大的电子表格.

You need drink a lot of tequila and punch yourself in the head repeatedly until you forget everything you ever knew about SQL. Otherwise, you will just end up with an object graph that is nothing but a big spread sheet.

有几种方法可以在 Core Data 中完成您想要的操作.通常,您会使用复合谓词构造 fetch,该谓词将返回特定用户在特定日期范围内发布的所有帖子.获取结果控制器对此特别方便.

There are several ways to accomplish what you want in Core Data. Usually you would construct fetch with a compound predicate that would return all post within a certain date range made by a specific user. Fetched results controllers are especially handy for this.

最直接的方法是设置对象图,例如:

A most straightforward method would be to set up you object graph like:

UserEntity
--Attribute username
--Relationship post <-->> PostEntity

PostEntity
--Attribute creationDate
--Attribute content
-- Relationship user <<--> UserEntity

然后在你的 UserEntity 类中有一个像这样的方法:

Then in your UserEntity class have a method like so:

- (NSArray *) mostRecentPost{
    NSPredicate *recentPred=[NSPredicate predicateWithFormat:@"creationDate>%@", [NSDate dateWithTimeIntervalSinceNow:-(60*60*24)]];
    NSSet *recentSet=[self.post filteredSetUsingPredicate:recentPred];
    NSSortDescriptor *dateSort=[[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
    NSArray *returnArray=[[recentSet allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:dateSort]];
    return returnArray;
}

当您想要按日期排序的特定用户的最新帖子列表时,只需调用:

When you want a list of the most recent post of a particular user sorted by date just call:

NSArray *arrayForDisplay=[aUserEntityClassInstance mostRecentPost];

...我是否只是通过每个帖子块数据(内容,创建日期)到邮政实体?我是否也通过了发布实体的用户名?如何用户实体知道何时创建新用户?

让我对它进行伪代码.您有两个定义 userObj 和 postObj 实例的类.当有新帖子出现时,您:

Let me pseudo code it. You have two classes that define instances of userObj and a postObj. When a new post comes in, you:

Parse inputPost for a user;
Search existing userObj for that name;
if userObj with name does not exist
    create new userObj;
    set userObj.userName to name;
else
    return the existing userObj that matches the name;
Parse inputPost for creation date and content;
Search post of chosen userObj;
if an exiting post does not match content or creation date
    create new postObj
    set postObj.creationDate to creation date;
    set postObj,content to content;
    set postObj.user to userObj; // the reciprocal in userObj is created automatically
else // most likely ignore as duplicate

您有单独的 userObj 和 postObj,因为虽然每个帖子都是独一无二的,但每个用户可能有很多帖子.

You have separate userObj and postObj because while each post is unique, each user may have many post.

要掌握的重要概念是您处理对象,即数据和逻辑的封装实例.这不仅仅是数据库中的行和列.例如,您可以编写托管对象子类,其中单个实例可以决定是否与另一个类的实例形成关系,除非达到对象的特定内部状态.dbs 中的记录没有那种逻辑或自主性.

The important concept to grasp is that your dealing with object i.e. encapsulated instance of data AND logic. This isn't just rows and columns in a db. For example, you could write managed object subclasses in which a single instance could decide whether to form a relationship with an instance of another class unless a specific internal state of the object was reached. Records in dbs don't have that sort of logic or autonomy.

掌握将对象图用于数据模型的最佳方法是不仅忽略 db,还忽略 Core Data 本身.相反,着手编写一个小型测试应用程序,您可以在其中手动编写所有数据模型类.不必详细说明每个类的几个属性以及对另一个类的某种引用.想想您将如何管理将数据解析到每个类,将类及其数据链接在一起,然后将其取回.手动执行一次或两次,对象图的性质就变得很明显了.

The best way to get a handle on using objects graphs for data models is to ignore not only db but Core Data itself. Instead, set out to write a small test app in which you hand code all the data model classes. It doesn't have to be elaborate just a couple of attributes per class and a reference of some sort to the other class. Think about how you would manage parsing the data out to each class, linking the classes and their data together and then getting it back out. Do that by hand once or twice and the nature of object graphs becomes readily apparent.

这篇关于对核心数据数据结果进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 14:47