私のデリゲートでは、NSManagedObjects の NSSet を取得するために次のメソッドを指定します。
- (NSSet *) entitiesForName : (NSString *)entityName matchingAttributes : (NSDictionary *)attributes {
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext: [NSThread isMainThread] ? managedObjectContext : bgManagedObjectContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity: entity];
NSMutableArray *subPredicates = [[NSMutableArray alloc] init];
[attributes enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
if ([value class] == [NSString class]) {
NSString *sValue = (NSString *)value;
[subPredicates addObject:[NSPredicate predicateWithFormat:@"%@ == '%@'", key, [sValue stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"]]];
} else {
[subPredicates addObject:[NSPredicate predicateWithFormat:@"%@ == %@", key, value]];
}
}];
NSPredicate *matchAttributes = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
NSLog(@"matchPredicate: %@", [matchAttributes description]);
[fetch setPredicate: matchAttributes];
NSError *error;
NSSet *entities = [NSSet setWithArray: [managedObjectContext executeFetchRequest:fetch error:&error]];
if (error != nil) {
NSLog(@"Failed to get %@ objects: %@", entityName, [error localizedDescription]);
return nil;
}
return [entities count] > 0 ? entities : nil;
}
次に、存在することがわかっているエンティティを使用してこのメソッドを開始し、同じ値の一部があることがわかっている属性に一致させます (sqlite ファイルを確認しました)。
[self entitiesForName:@"Lecture" matchingAttributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:@"attending"]]
コンソールは次のように出力します (述語を表示)。
2013-09-11 22:47:20.098 CoreDataApp[1442:907] matchPredicate: "attending" == 0
NSObject エンティティに関する情報:
- プロパティ "attending" は BOOL (クラスの NSNumber に変換されます)
- このテーブル (講義エンティティ) には多くのレコードがあり、半分は "attending" 値 0 で、残りの半分は 1
- メソッドの使用 -空のセットを返します。
その他の情報:
同じ方法で取得する別のメソッドが定義されていますが、述語なしで (すべての管理対象オブジェクトを取得する)、これは同じテーブルから機能します。私はこれを使用し、これから取得したレコードを分析すると、「出席」0と1があるものもあることが証明されます
質問:
セットが空に戻る原因となる -entitiesForName メソッドに何か問題がありますか?