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

The Archive Base Latest Questions

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

I am using a helper class in my app, to access a database and

  • 0

I am using a helper class in my app, to access a database and return an array of 5 objects, which I then assign to a property in my view controller.

In my view controller I call it like so:

- (void)viewDidLoad
{
[super viewDidLoad];

DatabaseHelper *helper = [[DatabaseHelper alloc] init];
self.trailersArray = [helper retrieveTrailers];

// Set trailer for first round
self.trailer = [self.trailersArray objectAtIndex:0];

// Prepare audio player
[self prepareToPlay];

// Swoop film screen in
[self swoopFilmScreenInAndAddPlayButton];

// Fade title in
[self fadeInTitleScreen];

// Initialise button array and set buttons to hidden
self.buttonOutlets = [NSArray arrayWithObjects:self.button1, self.button2, self.button3, self.button4, nil];
[self hideButtons];

// Initialise rounds
self.round = [NSNumber numberWithInt:-1];

// Initialise score which will also update graphics
[self initialiseScore:self.round];
self.scoreInteger = 0;

// Start first round
self.round = [NSNumber numberWithInt:0];

NSLog([self.trailersArray description]);
// User will trigger playing with Play IBAction

// User will trigger stop playing with Stop Playing IBaction

}

My problem is that once viewDidLoad is finished, the helper object seemingly disappears, as do its objects, and my self.trailersArray ends up pointing at nothing.

How do I fix this? Have tried deep copying, and using a retain attribute on the property but not working.

I can’t use a class method because it ruins my helper object database methods but I am intrigued as to how class methods get around this memory problem?

Thanks for any help.

Alan

EDIT: As requested, code for retrieveTrailers below:

-(NSArray *)retrieveTrailers
{
// Get list of mp3 files in bundle and put in array
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.mp3'"];
NSArray *onlyMP3s = [dirContents filteredArrayUsingPredicate:fltr];
NSMutableArray *arrayOfTrailersWithMP3s = [[NSMutableArray alloc] init];


// Query database for objects where (unique id) = (mp3 file title)
for(NSString *string in onlyMP3s)
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Trailer"];
    NSString *stringWithNoFileExtension = [string stringByReplacingOccurrencesOfString:@".mp3" withString:@""];
    request.predicate = [NSPredicate predicateWithFormat:@"unique = %@", stringWithNoFileExtension];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *array = [[self managedObjectContext] executeFetchRequest:request error:nil];

    Trailer *trailer = [array lastObject];
    NSLog([NSString stringWithFormat:@"Trailer Available:%@", trailer.title]);

    if(trailer)
    {
        [arrayOfTrailersWithMP3s addObject:trailer];
    }

}

// Choose five of the trailers at random and return

NSMutableArray *fiveRandomSelectedTrailers = [[NSMutableArray alloc] init];

int numberOfAvailableTrailers = [arrayOfTrailersWithMP3s count];

for(int i=0;i<5;i++)
{
    int rand = (arc4random() % (numberOfAvailableTrailers));

    Trailer *trailer = [arrayOfTrailersWithMP3s objectAtIndex:rand];
    NSLog([NSString stringWithFormat:@"Trailer Chosen:%@", trailer.title]);

    [fiveRandomSelectedTrailers addObject:trailer];

    [arrayOfTrailersWithMP3s removeObject:trailer];

    numberOfAvailableTrailers --;
}

return fiveRandomSelectedTrailers;

}

  • 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-31T22:34:38+00:00Added an answer on May 31, 2026 at 10:34 pm

    If that really is you viewDidLoad code what you are doing is creating a local object of your helper class which is then out of scope once the function has completed.

    If you have a retained property of the helper class, you don’t need the declaration: try doing it this way

    In your .h file have a line like this:

    @property(retain, nonatomic) DatabaseHelper *helper;
    

    In your .m file make sure you have:

    @synthesize helper;
    

    In your viewDidLoad:

    self.helper = [[DatabaseHelper alloc] init];
    self.trailersArray = [self.helper retrieveTrailers];
    

    This way you are creating an object of your helper class and assigning it to property, instead of creating a local variable. And, as you can see, you can use the property object of your helper class when you want to send it messages.

    I’m assuming you are using MRC instead of ARC.

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

Sidebar

Related Questions

I have a view helper file, app/helpers/analysis_helper.rb, whose toplevel methods I've been using in
is it possible to generate cross-application action links using the HTML helper class? I
I currently have a class file with the following enumeration: using System; namespace Helper
Currently I'm using the helper methods outlined here to return some JSON from my
i am creating an app that needs a database. i created it using sqlite
In my ASP.NET MVC app I'm using a small helper to iterate through all
I have a class library which is a set of helper classes to develop
There's this web app, which relies on a sort of data access library (simple
using Rails 3.1.0.rc4, I'm trying to access a route helper in a javascript file
I am creating an app that needs a database. I created it using sqlite

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.