ios - Core Data Predicate SQL Unable to Parse -
i've been banging head on getting filtering statement work. i've tried bunch of things (as can see in commented code). trying do search when user typing - searches customerobject.firstname, customerobject.lastname, , title - if phrase in of these show results. .lastname , .firstname properties of customerobject.
class func searchcontainspredicateestimateinvoice(searchstring: string, existingpredicate: nspredicate? = nil) -> nspredicate { var words = searchstring.componentsseparatedbystring(" ") var predicatelist = [nspredicate]() word in words { if count(word) > 0 { var str = nsstring(format: "subquery(%@, $f, $f.%@ contains[cd] %@) or subquery(%@, $l, $l.%@ contains[cd] %@) or (%@ contains[cd] %@)", "\(customer_object_key)","\(user_first_name_local_key)" ,word, "\(customer_object_key)", "\(user_last_name_local_key)", word, estimate_name_key, word) println(str) var pred = nspredicate(format: "subquery(%@, $f, $f.%@ contains[cd] %@) or subquery(%@, $l, $l.%@ contains[cd] %@) or (%@ contains[cd] %@)", "\(customer_object_key)","\(user_first_name_local_key)" ,word, "\(customer_object_key)", "\(user_last_name_local_key)", word, estimate_name_key, word) //var pred = nspredicate(format: "subquery(customerobject, $f, $f.firstname contains[cd] w).@count > 0") //var pred = nspredicate(format: "subquery(customerobject, $g, $g.firstname contains[cd] %@).@count > 0", word) //var str = nsstring(format: "subquery(%@, $f, $f.%@ contains[cd] %@) or subquery(%@, $l, $l.%@ contains[cd] %@) or (%@ contains[cd] %@)", "\(customer_object_key)","\(user_first_name_local_key)" ,word, "\(customer_object_key)", "\(user_last_name_local_key)", word, estimate_name_key, word) predicatelist.append(pred) } } if existingpredicate != nil { predicatelist.append(existingpredicate!) } return nscompoundpredicate(type: .andpredicatetype, subpredicates: predicatelist) } //var pred = nspredicate(format: "subquery(%k, $f, $f.%k contains[cd] %@) or subquery(%k, $l, $l.%k contains[cd] %@) or (%k contains[cd] %@)", "\(customer_object_key)","\(user_first_name_local_key)" ,word, "\(customer_object_key)", "\(user_last_name_local_key)", word, estimate_name_key, word)
this actual value of appears on log:
subquery(customerobject, $f, $f.firstname contains[cd] w) or subquery(customerobject, $l, $l.lastname contains[cd] w) or (name contains[cd] w)
data model:
there problems in predicate creation:
- subquery used in conjunction to-many relationships (and not needed).
- never use
nsstring(format:)
create predicates. escaping , quoting rules between string format strings , predicate format strings different. - use
%k
key path substitution, not%@
.
the predicate should therefore like
let pred = nspredicate(format: "%k.%k contains[c] %@ or %k.%k contains[c] %@", customer_object_key, user_first_name_local_key, word, customer_object_key, user_last_name_local_key, word)
assuming customer_object_key name of to-one relationship, , user_first_name_local_key/user_last_name_local_key attributes of target entity.
Comments
Post a Comment