title: Singleton class DataLoader - set values from Core Data in AppDelegate, however cannot access DataLoader variables in other classes
body: <p>I have been wracking my brain, and a lot of searching, but no luck so far. </p>
  
  <p>Basically, I have setup Core Data to load data from an sqlite db in my AppDelegate class into shared variables in a singleton class called DataLoader. </p>
  
  <p>I also have a main class called GameScene, which will also use DataLoader data.</p>
  
  <p>What I am trying to do is as follows:</p>
  
  <ul>
  <li>load data from DB (using the background thread) and store results in DataLoader singleton class variables (specifically an NSMutableArray)</li>
  <li>In this way the data is loaded while the scenes transition to main menu area</li>
  <li>In the GameScene class I try to access the data stored in the DataLoader singleton, and use this data in the game.</li>
  </ul>
  
  <p>My issue is that although I can see that I store objects in the AppDelegate class, I don't seem able to return the objects in the GameScene class. Usually ends in an EXC_BAD_ACCESS error.</p>
  
  <p>I am really looking for some example of something similar or if you have any ideas. </p>
  
  <p>If you need code samples, let me know. </p>
  
  <p>Thanks,
  Pras.</p>
  
  <p>*<strong><em>EDIT - Code Snippets</em>*</strong></p>
  
  <p>DataLoader.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "cocos2d.h"
  
  @interface DataLoader : NSObject {
  
  }
  
  //setup singleton
  +(DataLoader *) sharedDataLoader;
  @property (nonatomic, retain) NSMutableArray * veHint;//2 characters
  </code></pre>
  
  <p>DataLoader.m</p>
  
  <pre><code>#import "DataLoader.h"
  #import &lt;CoreData/CoreData.h&gt;
  
  @implementation DataLoader
  @synthesize veHint;
  
  static DataLoader * myDataLoader = nil;
  
  +(DataLoader *) sharedDataLoader{
  
      if(myDataLoader == nil){
          myDataLoader = [[[DataLoader alloc]init]retain];
      }
      return myDataLoader;
  }
  
  -(id) init
  {
      if((self = [super init])){
          veHint = [NSMutableArray arrayWithCapacity:10];
      }
      return self;
  }
  </code></pre>
  
  <p>AppDelegate.mm</p>
  
  <pre><code>#import "AppDelegate.h"
  #import "GameScene.h"
  #import "RootViewController.h"
  #import "Word.h"
  #import "DataLoader.h"
  -(void) applicationDidFinishLaunching:(UIApplication*)application
  {
  //other standard stuff here...
  
  //my load data method using data loader
  [self getRandomData];
  
  //other standard stuff here...
  
  // Run the intro Scene
  [[CCDirector sharedDirector] runWithScene: [GameScene node]];
  }
  
  //other standard methods for Core Data
  
  //Random data loader
  
  -(void) fetchRandomData: (NSString *) searchInteger{
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      NSLog(@"fetching random data for word size: %@", searchInteger);
  
      NSFetchRequest *request = [[NSFetchRequest alloc] init];
      NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"Word" inManagedObjectContext:managedObjectContext];
      [request setEntity:entity];
  
      //setup predicate
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K == %@", @"size", searchInteger];
      [request setPredicate:predicate];
  
      NSError *error = nil; 
      NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&amp;error]; 
      NSLog(@"fetch results count = %i", [fetchResults count]);
      [request release];
  
      NSMutableArray * results = [NSMutableArray arrayWithCapacity:10];
      NSMutableArray * fetchedIndexes = [NSMutableArray arrayWithCapacity:10];
      //get the total count, randomize selection of 10 results from the list
  
      int j = 0;
      while (j&lt;10) {
          int index = arc4random() % [fetchResults count];
          //get only unique values
          if ([fetchedIndexes containsObject:[NSNumber numberWithInt:index]] == false)     {
              [fetchedIndexes addObject:[NSNumber numberWithInt:index]];
  
              Word * word = (Word *) [fetchResults objectAtIndex:index];
              //NSLog(@"j = %i, index = %i, wordsize = %@, wordstring = %@",j, index, word.size, word.wordString);
              [results addObject:word];
              j++;
  
          }
      }
      //set dataloader 
      [[DataLoader sharedDataLoader] setVeHint:results];
  
      [pool drain];   
  
  }
  </code></pre>
  
  <p>Also, need to mention that the EXC_BAD_ACCESS is on the [pool drain] call in AppDelegate.mm.</p>
  
  <p>Thanks in advance for your help. </p>