Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6626669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:55:08+00:00 2026-05-25T21:55:08+00:00

What I have Message Entity user | text | date ———————————————- user1 | This

  • 0

What I have

Message Entity

user  | text                          | date 
---------------------------------------------- 
user1 | This is first message user 1  | 10:50     **
user2 | This is first message user 2  | 10:50     **
user3 | This is first message user 3  | 10:50     **
user1 | This is second message user 1 | 10:00
user4 | This is first message user 4  | 10:00     **
user2 | This is second message user 2 | 10:00
user3 | This is second message user 3 | 10:00
user1 | This is third message user 1  | 9:00
...

With that data I want to grab the first message of each distinct user using CoreData in a single fetch request.

That is

user1|This is the first message user 1|10:50
user2|This is the first message user 2|10:50
user3|This is the first message user 3|10:50
user4|This is the first message user 4|10:00

You can achieve that by grabbing first the distinct users, and then, perform a fetch request for each user using a predicate, but it will end with a lot of “queries” (5 in this case). It would be nice if it can be done in a single request.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:55:09+00:00Added an answer on May 25, 2026 at 9:55 pm

    I would add a relationship to your user entity called something like ‘lastMessage’ which you will always keep up-to-date whenever you insert a new message. Then the problem becomes trivial.

    In my experience it’s easier to de-normalize your schema slightly to improve performance rather than trying to create really complex queries.

    EDIT to show entities:

    ------------- 1  R1     * ------------
    |User Entity|<----------->| Message  |
    -------------             ------------
    | name      |------------>| text     |
    -------------    R2     1 | date     |
                              ------------
    

    You have two entities – User and Message. Set up 2 relationships between the entities:

    R1 – ‘messages’ bidirectional one-to-many (one user has many messages and one message belongs to one user)

    R2 – ‘lastMessage’ unidirectional one-to-one (one user has one last message)

    Keep R2 up-to-date whenever you add a new message.

    To get last messages for all users perform a fetch request for all users and then just look at the R2 relationship.

    Use setPropertiesToFetch: on the NSFetchRequest to populate the R2 relationship in a single trip to the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Message table and a User table. Both are in separate databases.
I have this error message: Msg 8134, Level 16, State 1, Line 1 Divide
I have a Message object associated with a User object (user_from and user_to). I
i've got this tables in my database. alt text http://img166.imageshack.us/img166/3133/testdbschemafy5.png With Entity Framework i
I have a web service , i add some extra class which have message
I have a message handler, which consumes from a JMS queue and that sends
I have 2 Message driven beans. 2 Activation Specs for these beans. I have
I have a message box with 3 buttons: Yes, No, Help: var result =
I have a message that I want to fadeIn when a form is successfully
In C++, a console application can have a message handler in its WinMain procedure,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.