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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:37:16+00:00 2026-05-26T22:37:16+00:00

Right now my app uses a pre-allocated NSMutableArray of ~40000 objects. When the app

  • 0

Right now my app uses a pre-allocated NSMutableArray of ~40000 objects. When the app hits this limit, it crashes due to an index out of bounds exception. I would like to eliminate this concern by re-using objects within a smaller array, lets say 900 items. This should give me enough room to be able to query previous objects within a certain time window.

I’m trying to avoid continuously growing the array, and I see 2 potential solutions (I’m using ARC and a backgroundSelector to periodically autosave the data):

1) keep removing old objects when new ones are inserted. I see the potential downside of having to continuously allocate objects. The benefit is that the data is in order, and I control the amount of elements in the array, accessing elements is also easy. Some of the app’s logic may break if I try to access an element that is beyond the bounds of the loop (for example a 900 element queue and I’m trying to access element (currentIndex -1200). Another downside is that this is not safe to use with multiple threads, if I try to iterate over the array to try to save some elements, it will crash if I try to dequeue the same array from a different thread.

NSMutableArray* queue;


- (NSNumber*) dequeue:(NSMutableArray*)queue {
    if ([queue count] == 0) return nil; // to avoid raising exception (Quinn)
    id headObject = [queue objectAtIndex:0];
    if (headObject != nil) {
        // so it isn't dealloc'ed on remove
        [queue removeObjectAtIndex:0];
    }
    return headObject;
}

// Add to the tail of the queue (no one likes it when people cut in line!)
- (void) enqueue:(NSNumber*)anObject forQueue:(NSMutableArray*) queue{
    [queue addObject:anObject];
    //this method automatically adds to the end of the array
}

2) Use some kind of a closed loop, and reset objects prior to use. Here any kind of array access logic would still work, although the results may be surprising. I also have not tested any of these methods.

NSMutableArray loopingArray;

- (id) getObjectAtIndex:(int)arrayIndex{
    id object =  [loopingArray objectAtIndex:arrayIndex %arraySize];  
    return object;
}

- (id) getObjectForWriting{
    [loopingArray resetForEvent:(++currentIndex %arraySize)];  
    id object =  [loopingArray objectAtIndex:currentIndex %arraySize];  
    return object;
}

- (id) getCurrentObject{
    id object =  [loopingArray objectAtIndex:currentIndex %arraySize];  
    return object;
}

-(void)resetForEvent:(int)event
{
   //get an object at index and ask it to reset itself 
}

3) Same as the queue, except the de-queued object is reset and is inserted at the head of the queue. This seems like the most reasonable solution.

How would you solve a problem like this? After writing this, it seems that the queue is an easier solution, even if it will require re-allocating the objects.

Updated:
I ended up using a circular buffer like data structure:

int count = [mutableArray count]
    [mutableArray replaceObjectAtIndex:((lastProcessedEpoch+1)%count) withObject:newDataPoint];
    lastProcessedEpoch++;

All subsequent accesses of the array use the current index % count. The call to replace can be changed to reset, but I dont want to write a reset method for ~30 variables within data point.

Thank you!

  • 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-26T22:37:17+00:00Added an answer on May 26, 2026 at 10:37 pm

    I’d use a circular buffer, which is similar to what what you’re suggesting for #2.

    I think your #1 method is probably simpler, though may be too slow, since the array might do a lot of work when you modify the head of the array. Watch your dequeue method; it needs to retain and autorelease the returned object before removing it from the queue.

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

Sidebar

Related Questions

I'm not sure if I actually need this right now but if my app
I have a Rails app that uses MySQL, MongoDB, NodeJS (and SocketIO). Right now,
I've got a rails 3 app in beta right now that uses Devise for
Right now my Android app uses an overlay to place a pin down from
I am making an iPhone app that uses a few NSArrays. Right now I
I'm developing a facebook app right now all by my lonesome. I'm attempting to
I am working on an app right now where I have Individuals, Athletes and
I've got an app that's in invite-only beta right now. Problem is, I can't
I'm developing a Grails App. I have about 20 Controllers right now and there
I'm deploying my Django app to another host/server using mod_wsgi and MySQLdb. Right now,

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.