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

  • SEARCH
  • Home
  • 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 1112817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:45:59+00:00 2026-05-17T02:45:59+00:00

So I got entities Level and Tile. Level has a to-many relationship with Tile.

  • 0

So I got entities Level and Tile. Level has a to-many relationship with Tile. Tile has a property ‘index’.

Right now I’m using this code to get the tiles array of Level sorted:

- (NSArray *)sortedTiles  
{  
    NSMutableArray *sortedTiles = [NSMutableArray arrayWithArray:[self.tiles allObjects]];  
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES];  
    [sortedTiles sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];  
    [sortDescriptor release];  
    return sortedTiles;  
}

This works, but I want to be able to retrieve a single Tile with a certain index, so I wrote this method in Level.h:

- (Tile *)tileWithIndex:(NSInteger)index;

The implementation is fairly simple:

- (Tile *)tileWithIndex:(NSInteger)index  
{  
    NSArray *sortedTiles = [self sortedTiles];  
    Tile *tile = [sortedTiles objectAtIndex:index];  
    return tile;  
}

Now, ofcourse this isn’t the most efficient way in doing so because the tiles array has to be allocated and sorted each time, so I was thinking: if I just add an instance variable to Level, ‘sortedTiles’, then I won’t have to rebuild it each time. But Level is a subclass of NSManagedObject, so is this possible and/or wise to do?

  • 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-17T02:46:00+00:00Added an answer on May 17, 2026 at 2:46 am

    I wouldn’t. The NSManagedObjects are a reflection of a record in the database and adding additional member variables outside the model strikes me as adding something that doesn’t belong.

    There are other, better ways to accomplish the same thing. The simplest would be to get the controller or delegate that’s fetching all these objects to fetch, sort and retain the array locally.

    For large or complex situations, you can use an NSFetchRequestController to collect, sort and dole out NSManagedObjects as needed. This integrates nicely into a UITableViewController. I haven’t done any performance testing, but for a situation where there are potentially a large number of records, I would try this first to see if the Fetch Results classes own cache management is sufficient.

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

Sidebar

Related Questions

i got 2 entities, Student and Phone, and a relationship one-to-many between them. @Entity
I've got two entities with a one-to-many relationship between them. The entity that holds
Say I got 3 entities: Business, Employee and Payment. A payment has a foreign
Got three entities - User - Has username/password, contact information, billing information etc. Periodical
I've got two entities in the entity framework. Now I want to seperate them
I'm working in Hibernate. I got 3 entities, that's the relationship among them: User
I got relation many to many between Restaurant and Tag. Here are my entities:
I'm using the Linq to Entities. I've got my main table, Employee setup with
Hi I got a problem with mapping my entities. I'm using JPA2 and Hibernate
I've got a OneToOne relation between two entities. This relation is eager by default,

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.