Is it possible to do a simple search using Core Data?

For sake of argument, lets assume that I were using a Master-Detail App project. My design concept is this:

On launch the user taps an Add button in the nav bar. In so doing:
1 User is able to enter a name for the next vacant row in the MVC table view and
2 this also creates a new table view associated with the newly named row.

The user can now populate the rows in the detail-view as necessary. So, IOW’s, each MVC row is associated with its own detail-view table view.

My question is this: Is it possible to perform a simple Search from the MVC file that the user can use to search for an item in any row in any detail-view table view?

I am aware as pointed out on p41, that if the ‘Allows External Storage’ is checked off in the .xcdatamodeld file that it is not possible to query CD, if I understood that correctly.

I have an app in mind that would need the functionality of a simple search such as I just described. If the search is not possible as described then I will abandon the app because the search would be the heart and sould of the app.

Any help will be much appreciated.

Thanks!

Yes.

Although you wouldn’t create a new detail tableView for each entry in your primary tableView. You would segue from each entry in your primary tableView to a single detail tableView and populate it according to which entry was selected.

Rather than searching, think of it more as filtering. If your primary tableView data list consists of say ‘A’ entities, and your detail tableViews each contain a bunch of ‘B’ entities belonging to some ‘A’ entity, then you’d just create a fetch request with an appropriate predicate.

e.g. Give me all of the ‘B’ entities such that “name == %@” where %@ is “Bobblehat”

@magnas

Thank you very much for the great advice.

I had to read your post several times. My brain wants to have a unique detailed view controller (DVC) for each row entry in the root view controller, but that is not what you are suggesting at all. I get that.

(For sake of discussion I will follow your lead and refer to the root view controller rows as box titles (‘A’ entities) and the contents of those boxes in the detailed view controller (DVC) as the ‘B’ entities.)

I was recently perusing a Dave Mark tutorial where he noted that it was much more expedient to not clutter up an an app with several DVC’s if it was not absolutely necessary to do so. This reduces the demand on memory and keeping everything in synch. Coupled with your comments this make perfect sense to me.

So, okay, I get this part. But, perhaps I don’t quite understand all that you wrote, so I’ll ask:

It is the user who will be doing the searching so how do I implement the search function so she will be able to find her ‘Core Data by Tutorials’ book that she saved in ‘Box #’ that she entered into a row in her primary table view?

I think I understand that when her ‘B’ entities are saved they will each have to be linked back to the appropriate ‘A’ entity row, so when she searches for ‘B’ she will get back the ‘A’ entity (box ID#) that her book is in. So the search will be confined to a single DVC which will allow the Search function to work?? (Did I just answer my own question?) :slight_smile:

Thanks for the great insight. You just made my day.

Yes, if I read you right I think you’ve got it.

For the data model we’ve talked about, it would be pretty simple. A box entity and a book entity. From the box entity you’d have a ‘to many’ relationship to the book entity, called something like ‘boxes’ and from the book you’d have a ‘to one’ relationship back to the box called something crazy like ‘box’. This embodies the idea that a box could contain any number of books but any single book could be in only one box at a time.

You would create your boxes and display them in your initial tableViewController. When you selected a box you would pass that box object through the segue to your DVC which would extract the books associated with that box (if any) and display them.

You could implement the search functionality in many ways but something straightforward would probably be best. Maybe a search bar at the top of your boxes table? When something is typed into it your code would search all of the book entities in Core Data for a book that matches the search term and then follow the ‘box’ relationship to determine the correct box. You could pop the result (i.e. the box name or whatever) into a popup or have your tableView show just that box. These are implementation details that are up to you as the developer obviously.

Anyway, I hope some of this helped.

What you have written is just what I have wanted to know and from what you have noted this all seems doable. I have intermediate CD skills so I understood your comments on that as well.

My biggest stumbling block is/was the implementation of the Search function, but at the moment I do have a good idea of how I need to design this which is a huge relief. And yes, it was my plan all along to put the usual generic Search button in the nav bar (or a tab bar, which would not be my preference.) of the RVC, so that is no problem.

So off the top of my head I would have a single Entity entitled: Boxes, with two Attributes: boxID and boxContents.

I am hoping that I can implement the search such that the user can just just type in her book title (or whatever) that she previously entered and have the box title (boxID) pop up in an Alert or table view row that will inform her which box contains her item of interest. Great!

I think I’m good to go.

Thank you very much for your help. I sincerely appreciate your time in helping me with this.

Pizza’s on the way! :smile: