Grouping the result of master data data?

I prototype the idea on the iPhone, but I'm at the crossroads of SQLite and CoreData. The main reason is that I cannot figure out how to group with the main data.

Essentially, I want to show the last published item, grouped by username. It is really easy to do in the SQL statement, but I could not get it to work in the master data. I believe that starting with the launch of a new application, I could try to do work with the main data, but this part is the main problem.

I added a predicate to my fetchrequest, but this gave me only the most recently added record, and not the most recently added record for each user.

The data model is pretty simple right now. It uses the following fields: username (string), post (string), created (datetime)

In short, are these types of queries possible with CoreData? I suppose if SQLite is under the hood, there must be some way to do this.

+2
source share
3 answers

, Core Data SQL. SQL " " Core Data. . - , - . Core Data SQL, SQL . Core Data SQL.

, .

, , - SQL. , , .

, Core Data. , , . .

- :

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

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

UserEntity :

- (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;
}

, , , :

NSArray *arrayForDisplay=[aUserEntityClassInstance mostRecentPost];

Edit:

... - (content, creationDate) ? ? , ?

. , userObj postObj. , :

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, , , .

, , , , . db. , , , , . dbs .

- db, . , . - . , , , . , .

+16

, SQLite Core Data SQLite. , , . (, , Core Data): " - , , ..."

, .

+1

"posts" - NSSet , :

NSDate *lastDate = [userInstance valueForKeyPath:@"@max.date"];

NSSet *resultsTemp = [setOfPosts filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"fecha==%@", lastDate] ];

Temp Post, .

0

All Articles