I currently have a UITableView with 2 sections that uses a NSFetchedResultsController. I am trying to work out how I display different entities in the different sections. I have a FOLDER objects and then also TAG objects. I am wanting to display all of these in each section, i.e. Section 1 all FOLDER, Section 2 all TAGS.
The relationship goes:
FOLDER (one to many)-> MOVIE (many to many)-> TAGS
How do I achieve this? Am I needing 2 separate tableView's or to use a single tableView with 2 different fetch requests? Please help!
EDIT: Fetch and tableView cellForRowAt code.
private let appDelegate = UIApplication.shared.delegate as! AppDelegate
private let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
private var fetchedRC: NSFetchedResultsController<Folder>!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
refresh()
}
private func refresh() {
do {
let request = Folder.fetchRequest() as NSFetchRequest<Folder>
request.predicate = NSPredicate(format: "name CONTAINS[cd] %#", query)
let sort = NSSortDescriptor(keyPath: \Folder.name, ascending: true)
request.sortDescriptors = [sort]
do {
fetchedRC = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
fetchedRC.delegate = self
try fetchedRC.performFetch()
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "folderCell", for: indexPath) as! FolderTableViewCell
let folder = fetchedRC.object(at: indexPath)
cell.update(with: folder)
cell.layer.cornerRadius = 8
cell.layer.masksToBounds = true
return cell
}
Use 2 FRC for your 2 sections.
One gets fed by your folder fetchrequest and the other by the tags, all in one tableview. Your tableview delegate methods take care of what you want to access. This is quite easy to handle that way. It only gets more complicated if you have more than just 2 sections.
That way your tableview delegate knows by section == 0 or 1 which FRC to access.
Related
I have read here that the way to fetch multiple entities with 1 NSFetchedResultsController is to use a parent child inheritance model. I have such a model:
https://imgur.com/a/nckHzvr
As you can see, TextPost,VideoPost, and ImagePost all have Skill as a parent entity. I am trying to make a single collectionView for which all three children show up. I am a little confused as to how to set the delegate methods though...
Here is the code for the view controller
class Timeline2ViewController: UIViewController {
#IBOutlet var postsCollectionView: UICollectionView!
var skillName: String?
fileprivate lazy var skillFetchedResultsController: NSFetchedResultsController<Skill> = {
let appDelegate =
UIApplication.shared.delegate as? AppDelegate
let managedContext =
appDelegate?.persistentContainer.viewContext
let request: NSFetchRequest<Skill> = NSFetchRequest(entityName: "Skill")
request.predicate = NSPredicate(format: "name == %#", self.skillName!)
let timeSort = NSSortDescriptor(key: "timeStamp", ascending: true)
request.sortDescriptors = [timeSort]
let skillFetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: managedContext!, sectionNameKeyPath: nil, cacheName: nil)
return skillFetchedResultsController
}()
override func viewDidLoad() {
super.viewDidLoad()
do {
try skillFetchedResultsController.performFetch()
} catch let error as NSError {
print("SkillFetchError")
}
}
}
extension Timeline2ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let sectionInfo = skillFetchedResultsController.sections?[section] else { return 0 }
return sectionInfo.numberOfObjects
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = postsCollectionView.dequeueReusableCell(withReuseIdentifier: "pcell", for: indexPath) as? PostViewCell else { return fatalError("unexpected Index Path")
}
let post = skillFetchedResultsController[indexPath.row] /* This is the line im not sure about
cell.background
return cell
}
}
Since only 1 entity is actually, returned, I am not sure how to access an element at a specific index path. For instance skillFetchedResultsController[indexPath.row] would I think only have 1 entity - the skill itself. I really want to be accessing its children. Do I have to somehow subclass skillFetchedResultsController and return only the children Im interested in?
Edit: with #pbasdf suggestions - I have this model:
Now when I create an entity like so:
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let textPost = NSEntityDescription.insertNewObject(forEntityName: "TextPost", into: managedContext) as! TextPost
textPost.text = "test text post"
try! managedContext.save()
and I setup my fetched results controller to look at "Post2" like so:
fileprivate lazy var skillFetchedResultsController: NSFetchedResultsController<Post2> = {
let appDelegate =
UIApplication.shared.delegate as? AppDelegate
let managedContext =
appDelegate?.persistentContainer.viewContext
let request: NSFetchRequest<Post2> = NSFetchRequest(entityName: "Post2")
// request.predicate = NSPredicate(format: "skill = %#", self.skill!)
let timeSort = NSSortDescriptor(key: "timeStamp", ascending: true)
request.sortDescriptors = [timeSort]
let skillFetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: managedContext!, sectionNameKeyPath: nil, cacheName: nil)
return skillFetchedResultsController
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
do {
try skillFetchedResultsController.performFetch()
} catch _ as NSError {
print("SkillFetchError")
}
}
I see no returned results in:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let sectionInfo = skillFetchedResultsController.sections?[section] else { return 0 }
return sectionInfo.numberOfObjects
}
Do I somehow need to link the two? Like create the Post object and the TextPost object at the same time? When I try and fetch TextPost objects directly it works.
I think the problem lies in your model. You should create a Post entity, and make it the parent entity of TextPost, VideoPost, and ImagePost. If your subentities have any attributes in common, move them from the subentities to the Post entity. Then establish a one-many relationship from Skill to Post.
Your FRC should fetch Post objects (which will by default include all the subentities), using a predicate if necessary to restrict it to those Post objects related to your desired Skill object, eg.
NSPredicate(format:"skill.name == %#",self.skillName!)
I've been struggling with this for several days already. There are similar problems in this website, but not very the same. And I didn't manage to go forward. I will try to simplify it with one variable.
Problem:
After filtering records in UITableView (records are taken from core data) and trying to push data to another viewcontroller, I get unfiltered index for data, so incorrect data is pushed to new view controller.
My code is below:
I set global variable for core data:
var events : [Event] = []
#objc func textFieldDidChange(_ textField: UITextField) {
if searchField.text == "" {
filterAdded = false
} else {
filterAdded = true
let request:NSFetchRequest<Event> = Event.fetchRequest()
let predicate = NSPredicate(format: "name CONTAINS[c] %# AND nearestDate >= %#", searchField.text!, currentCorrectDate! as CVarArg)
request.predicate = predicate
let sortDescriptor = NSSortDescriptor(key: "nearestDate", ascending: true)
request.sortDescriptors = [sortDescriptor]
do {
events = try DatabaseController.getContext().fetch(request)
}
catch {
print("Error: \(error)")
}
mainListOfDates.reloadData()
}
}
}
It is triggered every time some character is added to search field. UITableView name is "mainListOfDates".
This function works properly and calculated only filtered events:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return events.count }
This function shows all records from core data in UITableView cells:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as! EventTableViewCell
let event = events[indexPath.row]
cell.eventNameLabel.text = event.value(forKeyPath: "name") as? String
return cell
}
And with "didSelectRowAt" I would like to push filtered or unfiltered (works perfectly with unfiltered) data to new view controller:
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let eventStoryboard = Storyboard.instantiateViewController(withIdentifier: "EventViewController") as! EventViewController
let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as! EventTableViewCell
eventStoryboard.getEventName = events[indexPath.row].name ?? "nil"
self.navigationController?.pushViewController(eventStoryboard, animated: false) }
How to solve this issue and send filtered correct data to new view controller?
Thanks in advance.
I would like to understand why fetchBatchSize is not working correctly with NSFetchRequest.
On initial fetch, after fault array loaded, the data array are loaded in a loop by batchSize until all data is loaded rather than until just the data required is loaded. using coreData instruments or editing run scheme clearly shows all items data loaded in loops of batchSize number of items until all data loaded instead of only loading data of those rows appearing in tableView.
Expected result:
Expect all items in fault array to be loaded followed by only first batch as data to be loaded.
Actual result:
All items loaded in fault array and then all data loaded in looped batches of batch size prior to any scrolling.
Here is the sample project viewController I have created to demonstrate this:
import UIKit
import CoreData
class BatchTestTableViewController: UITableViewController {
var context: NSManagedObjectContext!
var items: [Item]?
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "BatchTableViewCell", bundle: nil), forCellReuseIdentifier: "BatchTableViewCell")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
self.context = appDelegate.persistentContainer.viewContext
self.initializeItems()
if items != nil {
if items!.count == 0 {
for i in 0..<10000 {
let entityDescription = NSEntityDescription.entity(forEntityName: "Item", in: context)
let item = Item(entity: entityDescription!, insertInto: self.context)
item.objectId = UUID().uuidString
item.itemId = UUID().uuidString
item.itemName = "\(i)"
}
try? context.save()
self.initializeItems()
}
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if items != nil {
return items!.count
}
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BatchTableViewCell", for: indexPath) as! BatchTableViewCell
let item = self.items![indexPath.row]
cell.itemNameLabel.text = item.itemName
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
// MARK: - Helper Methods
func initializeItems() {
let request = NSFetchRequest<Item>(entityName: "Item")
let messageSort = NSSortDescriptor(key: "itemName", ascending: true)
request.fetchBatchSize = 20
request.sortDescriptors = [messageSort]
do {
self.items = try context.fetch(request)
} catch {
print("Could not fetch \(error)")
}
}
}
And here is the read out from CoreData Instruments:
From the above image of coredata instrument it can be seen that all data is loaded in 20 batch size loop until all 10,000 items are loaded prior to any scrolling of the tableView.
fetchBatchSize doesn't limit your data fetching. It enables fetchRequest to fetch data in a batch of 20 in your case. You can use this feature to restrict the working set of data in your application. In combination with fetchLimit, you can create a subrange of an arbitrary result set. You should use fetchLimit together
request.fetchLimit = 20
From Apple doc
When the fetch is executed, the entire request is evaluated and the identities of all matching objects recorded, but only data for objects up to the batchSize will be fetched from the persistent store at a time. The array returned from executing the request is a proxy object that transparently faults batches on demand. (In database terms, this is an in-memory cursor.)
I am currently learn and creating a basic decision app. The basics of the app is to take user input for a category they would like to do and then all the things that want to fill that category with.
Now I am wanting to display the results on a table view which works but I also what to click on each individual category that they recently used and be able to see the things that they placed under ever category. I was getting everything that was being save to the Core Data but now I am trying to use NSPredicate to filter out what I need. When I run the App there is nothing in the table view.
mainName I have passed in from a different view controller to capture and set what the name of the category was to help filter the data. I was trying to use it in the predicate as a filter.
I don't know if what I am doing is right but help would be great. This is independent study project I am doing to help finish my degree and everything I know is self taught so far. If what I have is completely wrong please tell me. This is just one of the hundreds of different ways I have tried to get this right.
#IBOutlet weak var tableview: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return whats.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
let Doing = whats[indexPath.row]
cell.textLabel!.text = Doing.valueForKey("what") as? String
return cell
}
func loadData(){
let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
if let context = appDelegate?.managedObjectContext{
let fetchRequest = NSFetchRequest(entityName: "Doing")
let namePredicate = NSPredicate(format: "namesR.noun = '\(mainName)'")
(whats as NSArray).filteredArrayUsingPredicate(namePredicate)
fetchRequest.predicate = namePredicate
do {
let results =
try context.executeFetchRequest(fetchRequest)
whats = results as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
If you want use Core Data in your tableView based app? You can use NSFetchedResultsController class and their delegate protocol methods. This class specially designed for a tableView!
Official Documentation for NSFetchedResultsController class
From my understanding, you're saying you have a Category entity and an Item entity with many Item for each Category. In that case, in the view controller where you want to display the Items, you need to have a Category variable to use in your predicate so that you only get back the Items associated with that Category.
class ItemsTableVC {
var category: Category!
lazy var fetchedResultsController: NSFetchedResultsController = {
let fetchRequest = NSFetchRequest(entityName: "Item")
fetchRequest.sortDescriptors = []
fetchRequest.predicate = NSPredicate(format: "category == %#", self.category)
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.sharedContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
}
I'm using a TabBar Controller with 2 tabs - Bills Reminder, Bills Paid.
After setting the status of the records in Bills Reminder, and I went into Bills Paid tab, it isn't showing the bills that have been marked as paid.
I quit the app and relaunch it , then went into Bills Paid tab and it is displaying all the bills that has been paid correctly now.
I'm using viewWillAppear in my Bills Paid View controller.
BillsPaidTableViewController.swift
class BillsPaidTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var fetchResultController:NSFetchedResultsController!
override func viewWillAppear(animated: Bool) {
println("Hey I'm loading bills paid")
self.tableView.reloadData()
super.viewWillAppear(false);
self.tableView.tableFooterView = UIView(frame: CGRectZero)
}
/*
comments: viewDidLoad will only load once and will not load after subsequent tapping on the the tab
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
self.tableView.tableFooterView = UIView(frame: CGRectZero)
}*/
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return self.fetchedResultsController.sections?.count ?? 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
let sectionInfo = self.fetchedResultsController.sections![section] as! NSFetchedResultsSectionInfo
return sectionInfo.numberOfObjects
}
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fetchRequest = NSFetchRequest()
// Edit the entity name as appropriate.
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
let entity = NSEntityDescription.entityForName("Bills", inManagedObjectContext: managedObjectContext)
fetchRequest.entity = entity
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 20
// Edit the sort key as appropriate.
let predicate = NSPredicate(format: " paid == 1 ")
fetchRequest.predicate = predicate
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
var error: NSError? = nil
if !_fetchedResultsController!.performFetch(&error) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//println("Unresolved error \(error), \(error.userInfo)")
abort()
}
}
return _fetchedResultsController!
}
var _fetchedResultsController: NSFetchedResultsController? = nil
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("BillPaidCell", forIndexPath: indexPath) as! BillPaidTableViewCell
// Configure the cell...
self.configureCell(cell, atIndexPath: indexPath)
return cell
}
func configureCell(cell: BillPaidCell, atIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
cell.billName?.text = object.valueForKey("name")!.description
cell.billAmt?.text = convertToDecimal( (object.valueForKey("amount")!.description as NSString).doubleValue , 2, 2)
cell.dateDue?.text = object.valueForKey("date_due")!.description
cell.datePaid?.text = object.valueForKey("date_paid")!.description
}
}
Each time, I click on the Bills Paid tab, it will show the message "Hey I'm loading bills paid", no problem with this part. However, the table isn't loading although I'm using .reloadData().
Where did I go wrong with this part?
You should refetch data when click on paid tab. To do this you can simply add _fetchedResultsController = nil in viewWillAppear function before calling table reloadData.