私は本の助けを借りてコアデータを学習しています。コードがあります:Pro Coreデータブック、サンプルコードの質問
- (void)loadData {
// Pull the movies. If we have 200, assume our db is set up.
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Movie"
inManagedObjectContext:context]];
NSArray *results = [context executeFetchRequest:request error:nil];
if ([results count] != 200) {
// Add 200 actors, movies, and studios
for (int i = 1; i <= 200; i++) {
[self insertObjectForName:@"Actor" withName:
[NSString stringWithFormat: @"Actor %d", i]];
[self insertObjectForName:@"Movie" withName:
[NSString stringWithFormat: @"Movie %d", i]];
[self insertObjectForName:@"Studio" withName:
[NSString stringWithFormat: @"Studio %d", i]];
}
// Relate all the actors and all the studios to all the movies
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Movie"
inManagedObjectContext:context]];
NSArray *results = [context executeFetchRequest:request error:nil];
for (NSManagedObject *movie in results) {
[request setEntity:[NSEntityDescription entityForName:@"Actor"
inManagedObjectContext:context]];
NSArray *actors = [context executeFetchRequest:request error:nil];
NSMutableSet *set = [movie mutableSetValueForKey:@"actors"];
[set addObjectsFromArray:actors];
[request setEntity:[NSEntityDescription entityForName:@"Studio"
inManagedObjectContext:context]];
NSArray *studios = [context executeFetchRequest:request error:nil];
set = [movie mutableSetValueForKey:@"studios"];
[set addObjectsFromArray:studios];
}
}
[request release];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
主な質問は次のとおりです。コンテキストポインタを更新する必要がありますか? 私はどういう意味ですか:メソッドの始めにコンテキストへのポインタを取得します。次に、ループ内でこのコンテキストを取得し、そこで管理対象オブジェクトを挿入します(-insertObjectForName:withName :)。それから私はコンテキストポインタの更新を見て、その質問を持っています:それはある種のルールですか、私は同じ、またはちょうどまあまあではないコード例を実行する必要がありますか?なぜ古いポインタを使用できないのですか?
---編集---もう一つの質問は:
二allocsと唯一のリリース:ここでは、コード内の要求の法的初期化ということでしょうか?あなたの2番目の質問について
its just not-so-neat code example
私の意見で
次に、リクエストと結果配列を再作成する必要がありますか?または古いポインタでうまくいくでしょうか? – Matubo
私は本のWebサイトからソースコードをダウンロードし、それらの行を削除しました。それはアプリには影響しません。それはちょうどタイプミスかもしれません.. –
まだその本に掲載されているエラッタはありません... http://www.apress.com/9781430233558 –