CoreDataiPhoneアプリケーションを作成しようとしています。私が追跡しているエンティティの1つは車であり、各車の1つの属性は「メーカー」です。
私のアプリケーションの「車の編集」セクションには、以前にシステムに入力された固有のメーカーのそれぞれをロードする必要があるUIPickerViewがあります。私がやろうとしているのは、NSFetchRequestを作成して、一意の「manufacturer」属性の配列を取得し、それを使用してUIPickerViewにデータを入力することです。
私が遭遇している問題は、データストアにゼロレコードまたは100レコードがあるかどうかに関係なく、要素0で実行されたフェッチリクエストに値@""のレコードが常に1つあることです。
私はこれを間違ってやっていますか、それともこれを行う簡単な方法がありますか?簡単なSQLクエリを実行できたらいいのにと思います!!!
私のコードは以下の通りです:
// Populate the manufacturerNameList array
NSManagedObjectContext *moc = [self.selectedLease managedObjectContext];
NSEntityDescription *ed = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:moc];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:ed];
// Get only manufacturer and only uniques
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"manufacturer",nil]];
[fetchRequest setReturnsDistinctResults:YES];
// Sort by manufacturer in ascending order
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"manufacturer" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
self.manufacturerNameList = [moc executeFetchRequest:fetchRequest error:&error];
if (error) {
// Handle the error
}
NSLog(@"The count of self.propertyNameList is %i",[self.propertyNameList count]);
ありがとう!