UISearchController with UITableView backed by Core Data iOS 8 - uitableview

Although there seems to be plenty of information on UISearchControllers, many of the approaches I have found have been deprecated in iOS 8. Most tutorials I have found use UISearchDisplayController, which is deprecated, and even the documentation on UISearchController uses convenience methods that have also been deprecated. I'm looking for someone who knows how to successfully search items in a UITableView that is backed by core data in iOS 8.
Currently I have a table view that that fetches and displays points of interest saved from a map view. Pretty standard boilerplate stuff - just persisting and fetching entities. The challenging part of this is adding a UISearchController, which recently replaced the UISearchDisplayController,to the table view that is backed by core data (the tableview displays any point of interest entity persisted to the data store). Using core data instead of a standard array is where the complication begins, and there isn't sufficient documentation using this particular approach. Open to any and all suggestions.
To kick things off, lets consider that I have stored my fetched items from core data in a mutable array. How would we go about using that to filter our table view?
self.searchResults = [NSMutableArray arrayWithCapacity:[[self.fetchController fetchedObjects] count]];

You should be using a NSFetchedResultsController to display your Core Data backed data in a table view. Please read up on that first. Boilerplate available via Xcode templates.
For searching, you can e.g. just invalidate the fetched results controller and let it recreate it lazily. When creating it, add a predicate for the search term.
The UISearchController will simply help you keep track of the usual search event, i.e. search text change, scope changed, cancel etc.

Related

How to implement core spotlight indexing in core data?

I am having issue with making Core Data searchable using Core Spotlight in iOS. I checked Index For Spotlight for some attributes like name, date modified for a. I subclassed NSCoreDataCoreSpotlightDelegate and called setOption(spotlightDelegate, forKey: NSCoreDataCoreSpotlightExporter) on persistent container's store description before loading persistent stores. In the logs, CoreData prints that it has successfully initialised by NSCoreDataCoreSpotlightDelegate subclass, but I am not able to see any record in spotlight.
Do I have to set Spotlight Display Name for entity also?
I know this thread is old but I would recommend you check out https://developer.apple.com/wwdc21/10098. This session is all about the updates to the NSCoreDataCoreSpotlightDelegate in iOS 15.
Discover how Core Data can surface data from your app in Spotlight with as little as two lines of code.
Learn how to make that data discoverable in Spotlight search and to customize how it is presented to people on device.
See how to implement full-text search within your app, driven completely with the data indexed by Spotlight.
There is an associated sample application to demonstrate how these new capabilities work.

Saving and Filtering Favourites from a UITableViewController without Core Data

I am building a simple informational based app that provides the users with built in Leaflets and Images to view, on a variety of topics. It's a UITabBar -based application where the first tab is a list of Leaflets, the second is a list of Videos and the third is a list of Languages. Each tab is represented by a UITableViewController.
I would like to set up a favouriting mechanism where users can swipe across any leaflet or video in the UITableViewCell and "favourite" it. Whichever they favourite will then get added to the 4th tab called (unimaginatively), Favourites.
I'm not using Core Data with this application because there's no real need to do that and because I'm quite new to this concept, I'm wondering on the best way to save up to 50 favourites. How would I store this? Would I use something like a NSUserDefault for example?
It's a broad question because I've looked online and I'm not quite sure how to approach this.
I would really appreciate if anyone had any guides or thoughts on how to firstly save this and secondly, how to fetch the favourites for the favourites tab. Would/Could I use something like a NSFetchedResultsController even though I'm not using Core Data?
Should I just use Core Data with this, to save the favourites?
Any thoughts on this would really be appreciated.

Do I need a local array when using Core Data?

I'm currently writing my first iOS app and I'm trying to implement Core Data. Users create food recipes and I want them saved to persistent storage.
Currently, without Core Data, I'm putting the recipes into an array from a singleton.
When I implement Core Data can I do away with this and fetch straight from the data model every time I need to update my UITableView? In the viewDidLoad method, create an array from the current data in Core Data every time a user navigates to it?
Yes you can.
You should look at the class NSFetchedResultsController it is designed to do this.
There is a great Ray Wenderlich tutorial which covers everything including things like automatically updating the table when the core data model changes etc...

Fetch data from core data without section

I am new in iOS and core data. Can anyone give me a example to fetch data from core data through NSFetchedResultsController and display in UITableView
Thanks in advance :)
This question is almost too general for this platform. You are expected to at least try something yourself.
One obvious thing to do is to look at the documentation. You should have found out that the core data template in Xcode has a functioning table view based on NSFetchedResultsController.
Create new project
Choose master-detail
Check Core Data
Examine the master view controller

Use of singleton class and sqlite

Im new to objective-c and I've been reading up on singleton classes. I want to implement it into my logic but im not sure if its correct/possible/doable to do so, any advise would be appreciated.
At the moment i'm loading data from an xml feed, but i want to have control what data should be displayed based on which button is clicked. For example, buttonA would display IT news and buttonB would display celebrity news.
My thinking is to load the xml data into sqlite on application start in the background and display my buttons view at the same time using the singleton class. If the user pushes the button it will query the required table and display the content into a tableView.
Is this viable? If not, could you please advise whats the best way to go about this?
Thank you.
First of all you should reconsider organization of your data model. You've named sqlite on one hand and a global array on the other.
I would point you to Core Data to store your parsed data in a convenient way. Finally all you need is to query the Core Data db and fetch what you need. This would be more memory efficient than storing your data in a global array.
Have a look at Apple's Core Data tutorial or at this nice turorial: "superdb-core-data-app-with-sections"
To share a managed object context you can use a singleton. Have a look at this blog post, it provides a solution without a singleton by passing references of the managed object context down the relevant objects. It is created in the app delegate.

Resources