私は自分のmocから結果を得るのを助けるために、NSManagedObjectContextカテゴリの中に単純な関数を持っています。私は、mocインスタンスから提供されたperformBlockAndWaitブロックの中で結果を実行しています。私はオートレリースされた配列を返す必要があり、通常はmocのexecuteFetchがそれを行います。しかし、この場合は、__blockディレクティブで配列を宣言しても、ブロック内に配列を保持する必要があります。ブロックが終了すると解放されるようです。 これは正常ですか? ありがとうございます。ブロック内の変数の理解(__blockディレクティブ)
-(NSArray*)executeFetchWithEntityName:(NSString*)entityName predicate:(NSPredicate*)predicate{
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self];
if (!entity) {
NSLog(@"entity is nil in executeFetchWithEntityName: %@", entityName);
return [[[NSArray alloc]init]autorelease];
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
[fetchRequest setEntity:entity];
if(predicate){
[fetchRequest setPredicate:predicate];
}
__block NSError *error = nil;
__block NSArray *array = nil;
[self performBlockAndWait:^{
array = [self executeFetchRequest:fetchRequest error:&error];
[array retain]; <------ IF I DONT RETAIN HERE, IT CRASHES FURTHER ON
}];
NSLog(@"retain count: %i", array.retainCount);
if (error) {
NSLog(@"error");
array = nil;
}
[fetchRequest release];
return [array autorelease];
}
Re。あなたのretainCountの使用:http://www.whentouseretaincount.com/ –
@StephenDarlington - ついに!決定的なリファレンスリンク! – jrturton
jajaja excellent –