私はアプリの一部をAppleのCoreDataRecipesサンプルコードに基づいています。
http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html
いくつかの変更を加えた後、私は導入したはずのバグを追跡するのにかなりの時間を費やしましたが、アップルのコードに存在する2行のコードを削除することで解決しました。
NSManagedDataObjectレシピに作成者属性を追加しました。これは、私が知る限り、レシピがすでに持っている他の文字列属性と実装が同じです。IngredientDetailViewControllerによって制御されるモーダルビューに出入りした後、私の新しい属性はゾンビになりました。IngredientDetailViewControllerのdeallocメソッドは
- (void)dealloc {
[recipe release];
[ingredient release];
[super dealloc];
}
バグを突き止めた後、レシピと材料(別のNSManagedObject)のリリースをコメントアウトしたところ、アプリが機能しているように見えました。私のコードは、これらのリリース呼び出しの有無にかかわらず機能することを発見しました。バグは私が行った別の変更によって修正されたに違いありません。私は今疑問に思っています
- アップルのサンプルコードが元々このように書かれたのはなぜですか?
- NSManagedObjectレシピの元の属性についてはどうでしたか?つまり、dealloc呼び出しからのゾンビ化の影響を受けませんでしたか?
上記が私の無知を十分に示していない場合、私はObjective CとiPhoneの開発に不慣れであることを指摘する必要がありますが、ここで何が起こっているのかを本当に理解したいと思います。
コメントに応じて編集および更新:
これらの行のコメントを外してゾンビの作成を複製することはできません。明らかに、バグシューティング中の別の変更でうまくいきました。私が最初に尋ねたもののいくつかは現在無効ですが、機能がこれらの呼び出しの有無にかかわらず同じように見えるため、NSManagedObjectsのリリースの使用に関してさらに混乱しました。今の私の主な質問は、彼らがそこにいるべきかどうかということです。IngredientDetailViewに保存すると、クラッシュが発生していました。ヘッダーは次のとおりです。
@class Recipe, Ingredient, EditingTableViewCell;
@interface IngredientDetailViewController : UITableViewController {
@private
Recipe *recipe;
Ingredient *ingredient;
EditingTableViewCell *editingTableViewCell;
}
@property (nonatomic, retain) Recipe *recipe;
@property (nonatomic, retain) Ingredient *ingredient;
@property (nonatomic, assign) IBOutlet EditingTableViewCell *editingTableViewCell;
@end
およびsaveメソッド:
- (void)save:(id)sender {
NSManagedObjectContext *context = [recipe managedObjectContext];
/*
If there isn't an ingredient object, create and configure one.
*/
if (!ingredient) {
self.ingredient = [NSEntityDescription insertNewObjectForEntityForName:@"Ingredient"
inManagedObjectContext:context];
[recipe addIngredientsObject:ingredient];
ingredient.displayOrder = [NSNumber numberWithInteger:[recipe.ingredients count]];
}
/*
Update the ingredient from the values in the text fields.
*/
EditingTableViewCell *cell;
cell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
ingredient.name = cell.textField.text;
cell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
ingredient.amount = cell.textField.text;
/*
Save the managed object context.
*/
NSError *error = nil;
if (![context save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate.
You should not use this function in a shipping application, although it may be useful during development.
If it is not possible to recover from the error, display an alert panel that instructs the user to quit the
application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self.navigationController popViewControllerAnimated:YES];
NSLog(@"in ingredient detail save after ingredient pop; - recipe.author is %@", recipe.author);
}
私は新しいユーザーなので、ここにデータモデルのスクリーンショットを置くことはできません。そのため、ここにリンクがあります。データモデルのスクリーンショット
そして最後にレシピヘッダー:
@interface ImageToDataTransformer : NSValueTransformer {
}
@end
@interface Recipe : NSManagedObject {
}
@property (nonatomic, retain) NSString *instructions;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *overview;
@property (nonatomic, retain) NSString *prepTime;
@property (nonatomic, retain) NSSet *ingredients;
@property (nonatomic, retain) UIImage *thumbnailImage;
@property (nonatomic, retain) NSString *author;
@property (nonatomic) BOOL *isDownloaded;
@property (nonatomic) BOOL *isSubmitted;
@property (nonatomic, retain) NSString *uniqueID;
@property (nonatomic) float averageRating;
@property (nonatomic) float numberOfRatings;
@property (nonatomic, retain) NSManagedObject *image;
@property (nonatomic, retain) NSManagedObject *type;
@end
@interface Recipe (CoreDataGeneratedAccessors)
- (void)addIngredientsObject:(NSManagedObject *)value;
- (void)removeIngredientsObject:(NSManagedObject *)value;
- (void)addIngredients:(NSSet *)value;
- (void)removeIngredients:(NSSet *)value;
@end
再度、感謝します。