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 797471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:48:40+00:00 2026-05-14T22:48:40+00:00

Working on an app where I have a large collections of managed objects against

  • 0

Working on an app where I have a large collections of managed objects against which I want to fetch a few random instances.

My question is, is there any way I can use NSPredicate and NSFetchRequest to return several objects at random.

I saw that you could actually add a NSFetchRequest into the entity using the data modeler, any way to do the random fetch using this?

Also what would be the best method for determining the “count” of a table so I can set the bounds of the random number generator.

let me know if you need more details.

Thanks!

Nick

  • 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-14T22:48:41+00:00Added an answer on May 14, 2026 at 10:48 pm

    This may not be exactly how you implement this, but hopefully it will get you started.

    Somewhere in your header or at the top of your implementation file:

    #import <stdlib.h>
    #import <time.h>
    

    Elsewhere in your implementation:

    //
    // get count of entities
    //
    NSFetchRequest *myRequest = [[NSFetchRequest alloc] init];
    [myRequest setEntity: [NSEntityDescription entityForName:myEntityName inManagedObjectContext:myManagedObjectContext]];
    NSError *error = nil;
    NSUInteger myEntityCount = [myManagedObjectContext countForFetchRequest:myRequest error:&error];    
    [myRequest release];
    
    //
    // add another fetch request that fetches all entities for myEntityName -- you fill in the details
    // if you don't trigger faults or access properties this should not be too expensive
    //
    NSArray *myEntities = [...];
    
    //
    // sample with replacement, i.e. you may get duplicates
    //
    srandom(time(NULL)); // seed random number generator, so that you get a reasonably different series of random integers on each execution
    NSUInteger numberOfRandomSamples = ...;
    NSMutableSet *sampledEntities = [NSMutableSet setWithCapacity:numberOfRandomSamples];
    for (NSInteger sampleIndex = 0; sampleIndex < numberOfRandomSamples; sampleIndex++) {
        int randomEntityIndex = random() % myEntityCount; // generates random integer between 0 and myEntityCount-1
        [sampledEntities addObject:[myEntities objectAtIndex:randomEntityIndex]];
    }
    
    // do stuff with sampledEntities set
    

    If you need to sample without replacement, to eliminate duplicates, you might create an NSSet of randomEntityIndex NSNumber objects, instead of just sampling random ints.

    In this case, sample from an ordered NSSet, remove NSNumber objects as you pull them out of the bag, and decrement myEntityCount for the purposes of picking a random NSNumber object from the set.

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

Sidebar

Related Questions

I have made an android app which working fine. I implemented the Login functionality
I have rails app which are working perfectly in the local computer. But when
I have an app which was tested thoroughly and working fine on Android Gingerbread
I'm working on a iPhone app which has a pretty large UITableView with data
I am working on app in which I have to parse below JSON. Anyone
I have been working to deploy a relatively large Rails app (Rails 2.3.5) and
I'm working on a large Django app, the vast majority of which requires a
I am working on an app and i have to save some data in
I am working on a little WinForm app and have been trying to find
I am working on an android app and have to use remote database for

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.