レコードの取得


NSArray *FetchAllChefs(NSError **error) {
NSArray *chefs = nil;

// Create a request to fetch all Chefs.

NSEntityDescription *chefEntity = [NSEntityDescription entityForName:@"Chef" inManagedObjectContext:context];

NSFetchRequest *allChefsRequest = [[NSFetchRequest alloc] init];
[allChefsRequest setEntity:chefEntity];

// Ask the context for everything matching the request.
// If an error occurs, the context will return nil and an error in *error.

chefs = [context executeFetchRequest:allChefsRequest error:error];

// Clean up and return.

[allChefsRequest release];

return chefs;
}

  1. どこから取ってくるかNSEntityDescriptionで指定する
  2. NSFetchRequestを生成する
  3. 生成されたNSFetchRequestに対しNSEntityDescriptionを設定する
  4. 何も条件(where句)がなければそのままコンテキストに対しNSFetchRequest をexecuteFetchRequestで送る

でデータが取得できる模様。
戻ってくるのはNSManagedObjectの配列なので取り出すにはvalueForKey:を使う


コメントを残す

メールアドレスが公開されることはありません。

72 + = 73