私は脳を荒らしています。シングルトンクラスDataLoader - AppDelegateのコアデータから値を設定しますが、他のクラスのDataLoader変数にはアクセスできません
基本的に、私は、AppDelegateクラスのsqlite dbからDataLoaderというシングルトンクラスの共有変数にデータをロードするようにコアデータを設定しました。
私はGameSceneというメインクラスも持っています。GameSceneはDataLoaderデータも使用します。私がやろうとしています何
次のとおりです。データローダーシングルトンクラス変数(特にNSMutableArrayの)
- (バックグラウンドスレッドを使用して)DBからの負荷データとストア結果このように、シーンがメインメニューエリアに移動している間にデータがロードされる
- GameSceneクラスでは、DataLoaderシングルトンに格納されているデータにアクセスし、このデータをゲームで使用しようとします。
私の問題は、私は、私はAppDelegateクラスのオブジェクトを格納することがわかりますが、私はGameSceneクラスのオブジェクトを返すことができていないようだということです。通常、EXC_BAD_ACCESSエラーで終了します。
私は実際に何か似たようなものやアイデアがある場合は、いくつかの例を探しています。
コードサンプルが必要な場合は、お知らせください。
ありがとう、 Pras。
* EDIT - コードスニペット *
DataLoader.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface DataLoader : NSObject {
}
//setup singleton
+(DataLoader *) sharedDataLoader;
@property (nonatomic, retain) NSMutableArray * veHint;//2 characters
DataLoader.m
#import "DataLoader.h"
#import <CoreData/CoreData.h>
@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;
}
AppDelegate.mm
#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:&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<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];
}
また、EXC_BAD_ACCESSがAppDelegate.mmの[pool drain]呼び出しにあることに言及する必要があります。
ご協力いただきありがとうございます。
はい、サンプルコードは問題を明確にし、問題を見つけやすくします。 –
多くのことが人生で間違っている可能性があります...私はどれを選ぶかわかりません...-(あなたのシングルトンのデータにアクセスする方法と、シングルトンがどのように定義されているかを教えてください。 –
こんにちは、お返事ありがとうございます。このコードはRay WenderlichのCore Dataチュートリアル([http://www.raywenderlich.com/934/core-data-tutorial]に非常によく似ています。私は、NSFetchRequestを含むメソッドgetRandomData()を持っています。このメソッドでは、フェッチされた結果がフィルタリングされ、この このCore Dataメソッドからの逸脱はごくわずかなので、質問に答えたかどうかわかりません [1]:http://www.raywenderlich.com/934/core-data-tutorial-getting-started – prazzledazzle