簡體   English   中英

如何形成謂詞以從Core Data獲取包含Array中的字符串元素的對象

[英]how to form predicate to get objects from Core Data that contain string elements from Array

我需要創建一個謂詞以從核心數據獲取對象,我的實體“ Form”如下所示:

@interface EEForm (CoreDataProperties)
@property (nullable, nonatomic, retain) NSDate *date;
@property (nullable, nonatomic, retain) NSString *descriptor;
@property (nullable, nonatomic, retain) NSString *location;
@property (nullable, nonatomic, retain) NSMutableSet<Participants *> *participants;

實體“參與者”如下所示:

@interface Participants (CoreDataProperties)

@property (nonatomic, assign) NSInteger indexRow;
@property (nullable, nonatomic, retain) NSString *participant;
@property (nullable, nonatomic, retain) EEForm *form;

我想在參與者字段的基礎上獲取對象,該字段包含給定數組中的所有對象(此數組由字符串對象組成,並且它根據用戶選擇的內容而變化)。 提取請求由FetchResultController執行。 我將謂詞設置為初始化。

我以這種方式做到了,但是它包括了所有至少包含給定數組中一個對象的對象。

- (NSFetchedResultsController *)fetchResultController {
    if(_fetchResultController != nil) {
        return _fetchResultController;
    }

    NSPredicate *lPredicate = [NSPredicate predicateWithFormat:@"ANY participants.participant IN %@", _selectedForPredicatel];
    NSFetchRequest *lRequest = [[NSFetchRequest alloc]init];
    [lRequest setPredicate:lPredicate];
    NSEntityDescription *lEntity = [NSEntityDescription entityForName:@"Form" inManagedObjectContext:self.managedObjectContext];
    [lRequest setEntity:lEntity];
    [lRequest setFetchBatchSize:10];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"date" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
    [lRequest setSortDescriptors:sortDescriptors];
    NSFetchedResultsController *fetchResultController = [[NSFetchedResultsController alloc]initWithFetchRequest:lRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    fetchResultController.delegate = self;
    self.fetchResultController = fetchResultController;
    NSError *error = nil;
    if(![[self fetchResultController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    return fetchResultController;
}

您能否建議如何正確設置謂詞以過濾包含給定數組中所有元素的對象。

編輯:用於謂詞的數組如下所示:

   _selectedForPredicatel = [[NSMutableArray alloc]initWithObjects:@"Child",@"Adult", @"Group of people", @"Family", @"Teenager", nil];

每次用戶選擇用於過濾“ Form”對象的參數時,此數組都會更新,因此我需要根據它完成獲取請求。 因此,如果“表單”包含參與者:@“兒童”,@“成人”,但在_selectedForPredicatel數組中有對象:@“成人”,@“一群人”,在這種情況下,此“表單”不應被拿來。 僅應提取包含_selectedForPredicatel數組的兩個元素的“表單”。 非常感謝您的任何建議。

可以使用ALL代替ANY:

 NSPredicate *lPredicate = [NSPredicate predicateWithFormat:@"ALL participants.participant IN %@", _sortByParticipants];

這將檢查是否所有對象都在您的集合中。

如果對於每個用戶的選擇,至少有一個Participant具有匹配的participant屬性,則要包括這些表單。

如果參與者的名稱是唯一的(至少對於每個Form ),則可以對匹配進行計數並與用戶選擇的計數進行比較。 但是,如果沒有,則需要分別創建一個謂詞測試每個用戶的選擇,然后將結果合並為一個大謂詞:

NSPredicate *template = [NSPredicate predicateWithFormat:@"(SUBQUERY(participants, $p, $p.participant == $SELECTED).@count > 0)"];
NSMutableArray *predicateArray = [NSMutableArray array];
for (NSString *testString in _selectedForPredicatel) {
    NSDictionary *subsVars = @{@"SELECTED" : testString};
    NSPredicate *testPredicate = [template predicateWithSubstitutionVariables:subsVars];
    [predicateArray addObject:testPredicate];
}
NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicateArray];
NSLog(@"finalPredicate is %@", finalPredicate);
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"ListEntity"];
fetch.predicate = finalPredicate;

這里的template是一個虛擬謂詞,使用$SELECTED作為用戶選擇的占位符。 for循環使用它為每個用戶的選擇創建一個謂詞,並將其添加到可變數組中。 通過使用AND子句復合謂詞數組來構建最終謂詞。

這非常混亂(最終謂詞已經夠糟糕了,底層的SQL太可怕了),因此性能可能令人沮喪。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM