我刚刚接管的应用程序随机崩溃,出现“ALL 或 ANY 运算符的左侧必须是 NSArray 或 NSSet”错误。
崩溃时应用程序正在尝试从核心数据中读取。它不会一直崩溃,只是随机崩溃。我不确定是导致问题的 PREDICATE 还是两个线程访问核心数据?如果它是 PREDICATE,我认为它每次都会崩溃。他们已经多次迁移到 db 结构,所以也许其中一次迁移使对象处于奇怪的状态,并且当 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
......
}
=== 这里是调用栈 ====
*** 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
最佳答案
问题是这里不需要 ANY 运算符。正如 Martin R 所提到的,ANY 用于对多关系。所以如果你有这样的东西:
class Recipe
{
public String title;
}
class CookBook
{
public Array recipes;
}
如果您想找到所有具有特定标题的食谱的食谱,那么您可以使用谓词:
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
尤其要注意“在键路径中使用谓词”部分。希望对您有所帮助。
关于ios - ALL 或 ANY 运算符的左侧必须是 NSArray 或 NSSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24269546/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我明白了:x,(y,z)=1,*[2,3]x#=>1y#=>2z#=>nil我想知道为什么z的值为nil。 最佳答案 x,(y,z)=1,*[2,3]右侧的splat*是内联扩展的,所以它等同于:x,(y,z)=1,2,3左边带括号的列表被视为嵌套赋值,所以它等价于:x=1y,z=23被丢弃,而z被分配给nil。 关于ruby-带括号和splat运算符的并行赋值,我们在StackOverflow上找到一个类似的问题: https://stackoverflow
在Rails4.1中,ActiveRecorddestroy_all是否将整个函数包装在一个事务中?例如,如果我有一堆记录,我对其执行了destroy_all操作,它们对这些单独的对象运行了一些回调,其中一个失败了,整个操作会在那个时候回滚吗? 最佳答案 看起来不像:#Fileactiverecord/lib/active_record/relation.rb,line386defdestroy_all(conditions=nil)ifconditionswhere(conditions).destroy_allelseto_a.
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
问题是:除了在“OperatorExpressions”?例如:1%!2 最佳答案 是的,可以创建自定义运算符,但有一些注意事项。Ruby本身并不直接支持它,但是superatorsgem做了一个巧妙的把戏,将运算符链接在一起。这允许您创建自己的运算符,但有一些限制:$geminstallsuperators19然后:require'superators19'classArraysuperator"%~"do|operand|"#{self}percent-tilde#{operand}"endendputs[1]%~[2]#Out
考虑这个,它工作正常::>.to_proc.curry(2)[9][8]#=>true,because9>8然而,即使>是一个二元运算符,如果没有指定的元数,上面的代码将无法工作::>.to_proc.curry[9][8]#=>ArgumentError:wrongnumberofarguments(0for1)为什么两者不等价?注意:我特别想用提供的一个参数创建中间柯里化(Currying)函数,然后然后调用然后用第二个参数调用它。 最佳答案 curry必须知道传入的过程的数量,对吧?:-1来自arity的负值令人困惑,但基本上