ALL或ANY运算符的左侧必须是NSArray或NSSet

时间:2014-06-17 17:03:58

标签: ios objective-c core-data

我刚刚接手的应用程序随机崩溃,“ALL或ANY操作符的左侧必须是NSArray或NSSet”错误。

崩溃时的应用程序试图从Core Data读取。它不会一直随机崩溃。我不确定它是导致问题的PREDICATE还是两个线程访问Core Data?如果是预测,我认为每次都会崩溃。他们已经对数据库结构进行了几次迁移,因此可能有一次迁移导致对象处于奇怪的状态,并且当Core Data提取该对象时它会崩溃吗?

以下是谓词调用

+(NSString *)buildCompoundContainsStringForField:(NSString *)field searchTerm:(NSString *)search operator:(NSString *)operator
{
NSArray *parts = [search componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *partJoin = [NSString stringWithFormat:@"'%@ %@ contains[c]'", operator, field];
NSString *compoundContains = [parts componentsJoinedByString:partJoin];
NSString *predicateString = [NSString stringWithFormat:@"%@ contains[c] '%@'", field, compoundContains];
return predicateString;
}



    NSPredicate *titleSearch = [AStore searchTitlePredicate:search];
    NSPredicate *ingredientSearch = [AStore searchIngredientsPredicate:search];
    NSPredicate *recipeDecription = [AStore searchDescriptionPredicate:search];
    NSPredicate *searchMethod = [AStore searchMethodPredicate:search];


+ (NSPredicate *)searchIngredientsPredicate:(NSString *)search {
return [NSPredicate predicateWithFormat:[self buildCompoundContainsStringForField:@"ANY ingredients.desc"
                                                                       searchTerm:search
                                                                         operator:@"AND"]];
}

+ (NSPredicate *)searchTitlePredicate:(NSString *)search {
return [NSPredicate predicateWithFormat:[self buildCompoundContainsStringForField:@"ANY title"
                                                                       searchTerm:search
                                                                         operator:@"AND"]];
}

+ (NSPredicate *)searchDescriptionPredicate:(NSString *)search {
return [NSPredicate predicateWithFormat:[self buildCompoundContainsStringForField:@"ANY recipeDescription"
                                                                       searchTerm:search
                                                                         operator:@"AND"]];
}

+ (NSPredicate *)searchMethodPredicate:(NSString *)search {
return [NSPredicate predicateWithFormat:[self buildCompoundContainsStringForField:@"ANY method"
                                                                       searchTerm:search
                                                                         operator:@"AND"]];
}

===其崩溃的方法======

-(void) updateDictionaryWithWeight:(int)weight usingRequest:(NSFetchRequest *)request
{
    NSError *error;
    NSArray *results = [self.context executeFetchRequest:request error:&error];  <---- CRASHES/ERRORS HERE

......

}

===这里是CALL STACK ====

    *** First throw call stack:
    (
        0   CoreFoundation                      0x0000000103fbb495 __exceptionPreprocess + 165
        1   libobjc.A.dylib                     0x0000000103d1a99e objc_exception_throw + 43
        2   Foundation                          0x000000010351606b -[NSPredicateOperator performOperationUsingObject:andObject:] + 826
        3   Foundation                          0x0000000103515c1e -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 314
        4   Foundation                          0x0000000103515ae2 -[NSPredicate evaluateWithObject:] + 19
        5   CoreData                            0x0000000101df40aa -[NSManagedObjectContext executeFetchRequest:error:] + 2170
        6   CoreData                            0x0000000101e3b18b -[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:] + 395
        7   CoreData                            0x0000000101ea5ed3 __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 563
        8   libdispatch.dylib                   0x00000001042ef72d _dispatch_client_callout + 8
        9   libdispatch.dylib                   0x00000001042de5d0 _dispatch_barrier_sync_f_invoke + 57
        10  CoreData                            0x0000000101e3af92 _perform + 114
        11  CoreData                            0x0000000101e3ae2d -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] + 301
        12  CoreData                            0x0000000101df3a21 -[NSManagedObjectContext executeFetchRequest:error:] + 497
        13  Things                             0x000000010035ff40 -[AStore updateDictionaryWithWeight:usingRequest:] + 128
        14  Things                             0x000000010035cce8 -[AStore weightedThingsWithSearchString:andFilterFlags:sortedBy:] + 920
        15  Things                             0x00000001003b87a7 -[ASearchViewController doSearchWithCompletionBlock:] + 1319
        16  Things                             0x00000001003be466 -[ASearchViewController localRefresh:] + 198
        17  Things                             0x00000001003c17e1 __59-[ASearchViewController filterControllerDidFinish:]_block_invoke + 577
        18  Things                             0x000000010059624e __72+[UIView(mfw) presentIndicatorWithLoadingTitle:successTitle:completion:]_block_invoke + 174
        19  libdispatch.dylib                   0x00000001042dc851 _dispatch_call_block_and_release + 12
        20  libdispatch.dylib                   0x00000001042ef72d _dispatch_client_callout + 8
        21  libdispatch.dylib                   0x00000001042df3fc _dispatch_main_queue_callback_4CF + 354
        22  CoreFoundation                      0x0000000104019289 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
        23  CoreFoundation                      0x0000000103f66854 __CFRunLoopRun + 1764
        24  CoreFoundation                      0x0000000103f65d83 CFRunLoopRunSpecific + 467
        25  GraphicsServices                    0x0000000104b2bf04 GSEventRunModal + 161
        26  UIKit                               0x0000000102493e33 UIApplicationMain + 1010
        27  Things                             0x0000000100002463 main + 115
        28  libdyld.dylib                       0x00000001045405fd start + 1
        29  ???                                 0x0000000000000001 0x0 + 1
    )
    libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:9)

问题是这里不需要ANY运算符。正如Martin R所提到的,ANY与许多关系一起使用。所以如果你有类似的东西:

class Recipe
{
  public String title;
}

class CookBook
{
 public Array recipes;
}

你想要找到所有具有某个标题的食谱的CookBook然后你会使用谓词:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY recipes.title CONTAINS[c] %@", @"sh"];

如果您只想要与您的标题匹配的所有Recipe对象,则不需要ANY运算符。只需使用:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title CONTAINS[c] %@", @"sh"];

如果您想要更深入的解释,请阅读:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Predicates.pdf

请特别注意“使用带键路径的谓词”部分。希望这会有所帮助。