Searchbar with core data issues - swift

I have a search bar.And data displayed in labels with scrollview.
For ex:
core data Fields :
1.id
2.company
3.Employe Name
4.Address
If i type id,company or Employee Name in searchbar i want to dispaly associated results.
my code :
For search data :
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
var request = NSFetchRequest(entityName: "Agency")
request.returnsObjectsAsFaults = false
var countResult : NSArray = context.executeFetchRequest(request, error: nil)!
let result = NSPredicate(format: "SELF CONTAINS[c] %#",searchText)
self.filtered = self.countResult.filteredArrayUsingPredicate(result!)
if (filtered.count == 0 ) {
searchActive = false;
}else {
searchActive = true;
}
println(filtered)
}
It shows an error " 'Can't use in/contains operator with collection".
These codes cannot satisfy what i want.And also i dont have a idea how to fetch the related rows according to enter value in search bar.
Thanks in Advance.

The first problem is your predicate - you're trying to use CONTAINS on an NSManagedObject subclass, but CONTAINS only works with String. To check whether your search text is contained within any of your managed objects you need to evaluate whether it is contained in each attribute (in your case id, company and empolyeeName, I'm assuming they're all Strings).
To do this you should change your predicate to:
let searchPredicate = NSPredicate(format: "id BEGINSWITH %# OR
company BEGINSWITH %# OR
employeeName BEGINSWITH %#",
searchText, searchText, searchText)
I would recommend using BEGINSWITH instead of CONTAINS[c] since when searching your user is likely to be entering the first part of the phrase. Also, as Apple said in their 2013 WWDC talk Core Data Performance Optimization and Debugging -
...we've got begins with and ends with and that's by far the cheapest query that you can execute.
...
Contains is more expensive because we have to work along and see
whether it contains...
And in the case of a search, you want it to be fast!
Secondly, you don't need to filter your results after getting them back from CoreData. You can set the predicate property on your NSFetchRequest and your returned results will be filtered. For example:
let request = NSFetchRequest(entityName: "Agency")
request.predicate = // Your predicate...
let results = context.executeFetchRequest(request, error)
// Now do what you need with the results.
A final note, it's best not to force unwrap your results from executeRequest in case there is some problem and nil is returned - in that case your app would crash. You could instead use:
if let unwrappedResults = results {
// Now do what you want with the unwrapped results.
}

I suspect it has something to do with your use of SELF in the predicate format, and the "collection" referred to in the error message is the controller sub/class within which your code resides.
Try something like this (forgive me I'm Obj-C not Swift so may have the syntax incorrect).
let searchAttribute = <<entity.attribute key path>>
let result = NSPredicate(format:"%K CONTAINS[cd] %#",searchAttribute, searchText)
Where %K refers to the key path, that in the case of Core Data is your entity attribute. For example: Agency.name if that attribute exists for your Agency object.
Read about Predicate Format String Syntax.
UPDATE after third comment...
In my apps my solution includes the creation of a custom method in an extension of the Core Data generated NSManagedObject subclass. If that sounds like you know what I mean, let me know and I will post details.
In the meantime, create a custom method in whatever class your UISearchBar is controlled... (apologies Obj-C not Swift)
- (NSString *)searchKey {
NSString *tempSearchKey = nil;
NSString *searchAtrribute1 = Agency.attribute1;
NSString *searchAtrribute2 = Agency.attribute2;
NSString *searchAtrribute3 = Agency.attribute3;
NSString *searchAtrribute4 = Agency.attribute4;
tempSearchKey = [NSString stringWithFormat:#"%# %# %# %#", searchAtrribute1, searchAtrribute2, searchAtrribute3, searchAtrribute4];
return tempSearchKey;
}
You'll obviously need a strong reference for your Agency entity object to persist within the class, otherwise you will need to embed this bit of code into your searchBar function.
Work OK?

Related

Fetch Request Predicate to filter a Core Data entity where a small NSSet is contained within larger NSSet

I'm writing a 100% SwiftUI app with iOS and macOS targets, using Core Data and NSPersistentCloudKitContainer to backup to iCloud and sync between devices signed into the same AppleID.
The three entities involved are: Meals, Portions, Foods
Each Meal has:
a many-to-many relationship with Portions
a many-to-many relationship with Foods
Each Portion has:
a many-to-many relationship with Foods
I'm attempting to prepare a predicate to filter meals where each meal portion contains a certain food OR the meal contains a certain food directly.
So I'll provide a practical example...
Meal 1
consists of...
Portions
Banana Smoothie
Egg Sandwich
Foods
Apple
The Portion with the name Banana Smoothie contains the following Foods:
Banana
Cows Milk
Honey
Meal 2
consists of...
Portions
Blueberry Smoothie
Ham Sandwich
Foods
Banana
For the macOS target, I'm using the relatively new Table structure to present a table that lists all Meal entities for a certain Food entity, including those Meal entities where one or more of the Portion entities contains that certain Food entity.
If I refer back to the above example, for the Food entity named "Banana", I'd want my predicate to filter my FetchRequest such that Meal entities with names "Meal 1" & "Meal 2" are in the results.
#FetchRequest var meals: FetchedResults<Meal>
Here is the current predicate for this FetchRequest...
let portions = NSSet(object: food.foodPortions as Any)
let predicatePartA = NSPredicate(format: "%# IN mealFoods", food)
let predicatePartB = NSPredicate(format: "ANY %# IN mealsPortions", portions)
let predicate = NSCompoundPredicate(orPredicateWithSubpredicates: [predicatePartA, predicatePartB])
where food is #ObservedObject var food: Food
and mealFoods and mealsPortions are NSSet many-to-many relationships to from every Meal object.
predicatePartA works fine, I suspect because it is one single Object IN an NSSet of objects.
predicatePartB doesn't crash, but it also doesn't resolve any meals, I suspect because I'm providing a set instead of a single object.
I've attempted to research for some time now how this might be achieved and the best I can come up with are the operators...
#distinctUnionOfSets
#"SUBQUERY()
...but apart from this website there is little documentation I can find on how to implement them.
UPDATE
With help from #JoakimDanielson I've attempted to use SUBQUERY...
let predicatePartB = NSPredicate(format: "SUBQUERY(mealsPortions, $portion, $portion IN %#).#count > 0", portions)
AND
let predicatePartB = NSPredicate(format: "SUBQUERY(mealsPortions, $portion, $portion IN %#).#count != 0", portions)
Again this does not crash, but it does not provide the expected results for the fetch request.
Any suggestions please?
Also worth noting that I've found some better documentation by Apple that supports this syntax although, because the predicate isn't working, I still not sure it is correct.
init(forSubquery:usingIteratorVariable:predicate:)
with the syntax
SUBQUERY(collection_expression, variable_expression, predicate);
Short answer...
let portions = food.foodPortions
let predicatePartB = NSPredicate(format: "(SUBQUERY(mealsPortions, $p, $p IN %#).#count != 0)", portions!)
OR, if I prepare a computed property for portions...
var portions: NSSet {
if let p = food.foodPortions { return p }
return NSSet()
}
then in the creation of the predicate I'm not required to force unwrap the optional NSSet...
let predicatePartB = NSPredicate(format: "(SUBQUERY(mealsPortions, $p, $p IN %#).#count != 0)", portions)
Most people reading this probably won't want to know the detail but nonetheless I feel compelled to write this down, so the long answer is...
... in two parts, or at least recognises two contributors who helped me solve it.
Part 1
Primarily #JoakimDanielson for confirming that SUBQUERY was the right path to a solution and for taking the time to work out the syntax for my case and also for questioning what eventually turned out to be a very basic error, the problem was not my SUBQUERY syntax but in fact the manner in which I was preparing the NSSet that I used in the predicate string.
All I needed to do was change...
let portions = NSSet(object: food.foodPortions as Any)
to...
let portions = food.foodPortions
after which I could either force unwrap it in the creation of the predicate, or otherwise prepare a computed property (the solution I chose) - as detailed above in the short answer.
This was simply an error as a result of my inadequate understanding of the collections NSSet and Set. A refresher of the swift.org docs helped me.
Part 2
Secondly this SO Q&A "How to create a CoreData SUBQUERY with BETWEEN clause?" and the reference to this clever article titled "SUBQUERY Is Not That Scary" by #MaciekCzarnik.
I went through the process of reducing the necessary iteration until I could line for line compare the SUBQUERY syntax. While it didn't actually solve my problem, this did encourage me to try numerous predicate syntax alternatives until I returned with an understanding of SUBQUERY and was able to confirm the original syntax was correct. It provided me with the type of example my brain can comprehend and work through to develop an understanding of how SUBQUERY actually works.
Because you have nothing better to read at the current moment in time...
var iterationOne: [Meal] {
let meals: [Meal] = []//all meals
var results = [Meal]()
for meal in meals {
var portionsMatchingQuery = Set<Portion>()
if let mealPortionsToCheck = meal.mealsPortions {
for case let portion as Portion in mealPortionsToCheck {
if portions.contains(portion) == true {
portionsMatchingQuery.insert(portion)
}
}
if portionsMatchingQuery.count > 0 { results.append(meal) }
}
}
return results
}
can be simplified to _
var iterationTwo: [Meal] {
let meals: [Meal] = []//all meals
let results = meals.filter { meal in
var portionsMatchingQuery = Set<Portion>()
if let mealPortionsToCheck = meal.mealsPortions {
for case let portion as Portion in mealPortionsToCheck {
if portions.contains(portion) == true {
portionsMatchingQuery.insert(portion)
}
}
return portionsMatchingQuery.count > 0
}
return false
}
return results
}
can be simplified to _
var iterationThree: [Meal] {
let meals: [Meal] = []//all meals
let results = meals.filter { meal in
let portionsMatchingQuery = meal.mealsPortions?.filter { portion in
for case let portion as Portion in meal.mealsPortions! {
return portions.contains(portion) == true
}
return false
}
return portionsMatchingQuery?.count ?? 0 > 0
}
return results
}
can be simplified to _
var iterationFour: [Meal] {
let meals: [Meal] = []//all meals
let results = meals.filter { meal in
meal.mealsPortions?.filter { portion in
for case let portion as Portion in meal.mealsPortions {
return portions.contains(portion) == true
}
return false
}.count ?? 0 > 0
}
return results
}
iterationFour == predicatePartB

Core data Fetch request on relationship properties

My app has a number of wordLists each of which contain a number of words. In one view controller a tableView lists the wordLists and then a sub view controller has a tableView with the words in it. The segue passes the wordList entity. But I cannot work out how to do a fetch request on the wordList passed to then get all the words. Error message is shown below. I need to do a fetch request rather than looking at properties so I can do a sort.
Thanks in advance for any help on this one....
The WordList entity has an attribute listName, and relationships: words, destination: Word, Inverse: wordList
The Word entity has an attribute wordName, wordIndex and relationships: wordList, destination: WordList, Inverse: words
My WordsViewController looks like:
class WordsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
var coreDataStack: CoreDataStack = (UIApplication.sharedApplication().delegate as! AppDelegate).coreDataStack
var wordList: WordList?
var words = [Word]()
var word: Word?
override func viewDidLoad() {
super.viewDidLoad()
// set the title of the scene
title = wordList?.listName
// fetch all the words that are part of the wordList passed to this VC.
let fetchRequest = NSFetchRequest(entityName: "Word")
let wordListPredicate = NSPredicate(format: "word.wordList == '\(wordList)'") // GIVES AN ERROR SAYING UNABLE TO PARSE THE FORMAT STRING "word.wordList == 'Optional(<WordList:0x...
let sortDescriptor = NSSortDescriptor(key: "wordIndex", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate = wordListPredicate
do {
if let results = try coreDataStack.managedObjectContext.executeFetchRequest(fetchRequest) as? [Word] {
words = results
}
} catch {
fatalError("There was an error fetching system person")
}
}
You cannot use string interpolation to build a predicate, use
argument substitution instead. In your case (since var wordList: WordList? is an
optional):
if let theWordlist = wordlist {
let wordListPredicate = NSPredicate(format: "wordList == %#", theWordlist)
} else {
// wordlist is nil ...
}
Note also that "word.wordList == " in the predicate should be
"wordList == ", because "wordList" is the property of the "Word"
entity.
For more information, see Predicate Format String Syntax
in the "Predicate Programming Guide".
You should use a NSFetchedResultsController, not fetch all data in viewDidLoad. The fetched results controller will help you fetch and display the data efficiently, react to changes, optimize memory, etc.
In the fetched results controller's fetch request (which fetches entity Word), use this predicate (with a safeguard if your optional wordList is nil):
request.predicate = wordList != nil ?
NSPredicate(format: "wordlist = %#", wordList!) :
NSPredicate(value: false)
This assumes that there is a one-to-many relationship between word list and its words. I would recommend this setup, even if it implies that you might have to store the same word more than once if it is in different lists.
NB: the false predicate ensures you get a valid fetch request which will fetch nothing.

Core data fetch request not working

I have ShopItem and Product tables. Product has relation to ShopItem. I want to get all products from ShopItem object. I have created a method at ShopItem class
class ShopItem: NSManagedObject {
func fetchProducts(q: String) {
// some code ...
let fetchRequest = NSFetchRequest(entityName: "Product")
fetchRequest.predicate = NSPredicate(
format: "shopitem == %# AND keyword == %#",
self.objectID, String(jsonObj["keyword"])
)
fetchRequest.fetchLimit = 1;
do {
fetchResults = try self.managedObjectContext!.executeFetchRequest(fetchRequest) as! [Product]
} catch {
fatalError("Fetching from the store failed")
}
}
}
At logs it generates me the following sql:
SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZPRODUCT_TITLE, t0.ZPRODUCT_URL,
t0.ZSHOPITEM FROM ZPRODUCT t0 WHERE ( t0.ZSHOPITEM = ? AND
t0.ZKEYWORD = ?) LIMIT 1
And results is always empty.
If you have a relationship, why the fetch request? If everything is set up correctly, you just filter your products by keyword.
// filter by kw
let filteredProducts = shopItem.products.filter { $0.keyword == kw }
Your fetch request looks correct (although you don't need to pass self.objectID in your predicate, self is sufficient). When I see mysteriously empty relationships it's typically one of two things:
You aren't setting the relationship at creation like you think you are (or whatever even relates the two entities).
You are setting the relationship, but you have a to-one relationship configured in your data model instead of a to-many, which means only the last relationship you create will be present.
jsonObj["keyword"] returns an optional.
The String representation will look like Optional("keywordValue")
Unwrap the optional or use optional bindings if the value can be nil.

Compare string with CoreData

I have a String (for example "Test") and I also have a few strings which are stored in CoreData. Now I'd like to check if the string "Test" also exists in CoreData. So I simply want to compare all strings which are saved in CoreData with the string "Test".
Does someone of you know how to do this in Swift?
I don't know of any one-line solution, however you can:
Fetch with predicate
See if the returned array is empty (it doesn't exist in core data) or not (element exists).
var request : NSFetchRequest = NSFetchRequest(entityName: "Core_Data_Entity");
request.predicate = NSPredicate(format: "Property_name = %#", #"Test")
var results : [NSManagedObject] = context.executeFetchRequest(request, error: nil) as [Core_Data_Entity]
if (results.count > 0) {
//Element exists
}
else {
//Doesn't exist
}

NSPredicate with Swift and Core Data

i have a Core Data Object and i have 2 Fieds (one String(GUID) and one Int which i want to use as Filter)
So in SQL it would be "SELECT * FROM Answers WHERE qIndex = 1 AND GUID = '88bfd206-82fb-4dd0-b65d-096f8902855c'
Ive tried it with Core Data but i am not able to Filter with the String Value.
Here is my Code
var request = NSFetchRequest(entityName: "Answers")
request.returnsObjectsAsFaults = false;
let resultPredicate1 = NSPredicate(format: "qIndex = %i", qIndex)
let resultPredicate2 = NSPredicate(format: "formUUID = %s", formUUID)
var compound = NSCompoundPredicate.andPredicateWithSubpredicates([resultPredicate1, resultPredicate2])
request.predicate = compound
var results:NSArray = context.executeFetchRequest(request, error: nil)
Any ideas what i am doing Wrong? With the Same Code and Filter for 2 Integer Values it works fine.
Thanks in Advance
If formUUID is an NSString or a Swift String then you have to use the
%# placeholder:
let resultPredicate2 = NSPredicate(format: "formUUID = %#", formUUID)
This is not the exact response to your question, but a problem people might now encouter with your code now:
In the latest version of XCode, you must now unwrap the predicate, like this:
var compound = NSCompoundPredicate.andPredicateWithSubpredicates([predicate1!, predicate2!])
because NSPredicate initializer now return NSPredicate? type.
Instead of worrying about %# conversions and then composing AND predicates, you can use the PredicatePal framework:
let compound = *(Key("qIndex") == qIndex && Key("formUUID") == formUUID)
Assuming that qIndex and formUUID are the correct type, Swift will automatically deduce the correct types for the Key objects.