I have a button that calls clearCoreDataStore(). However, after it gets executed the view does not update, it still shows content. How can I update that?
On a related note, how can I update the view after I got data inserted to the app's Core Data stack?
I have the following 'fetching' code outside the class declaration:
fileprivate var fetchedResultsControllerForMapEntity:
NSFetchedResultsController<Map> = {
// Create Fetch Request
let fetchRequest: NSFetchRequest<Map> = Map.fetchRequest()
// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
// Create Fetched Results Controller
let fetchedResultsControllerForMapEntity =
NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultsControllerForMapEntity
}()
fileprivate var fetchedResultsControllerForCoordinateEntity: NSFetchedResultsController<Coordinate> = {
// Create Fetch Request
let fetchRequest: NSFetchRequest<Coordinate> = Coordinate.fetchRequest()
// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "latitude", ascending: true)]
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "longitude", ascending: true)]
// Create Fetched Results Controller
let fetchedResultsControllerForCoordinateEntity = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultsControllerForCoordinateEntity
}()
and on viewDidLoad, I do:
do {
try fetchedResultsControllerForMapEntity.performFetch()
try fetchedResultsControllerForCoordinateEntity.performFetch()
} catch
Related
No matter what I put as the value in sectionNameKeyPath: "value", the fetched objects get returned in the order in which they were saved in the database. The number of sections is correct, but whether I view the fetched objects by section fetchedResultsController.sections or fetchedResultsController.object(at: indexPath) I get the same results.
This is in the UITableViewController
lazy var fetchedResultsController: NSFetchedResultsController<Dice> = {
let fetchRequest: NSFetchRequest<Dice> = Dice.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: false)]
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
// Doesn't matter what I set in the "sectionNameKeyPath" value
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: "set.identifier", cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
Has anyone else seen this?
I followed this suggestion:
Format sectionNameKeyPath NSFetchedResultsController - Swift
I'm having a bit of a problem with this implementation.
Using Xcode 8 and iOS 10.
I set Codegen to Manual/None and added my NSManagedObject Subclasses.
Then I added as you suggested:
var sectionIdentifier: String
{
get
{
let dateFormatter = DateFormatter()
// dateFormatter.dateStyle = .short
// dateFormatter.timeStyle = .none
// ..or to stick to your original question:
dateFormatter.dateFormat = "yyyy-MMM-dd"
return dateFormatter.string(from: date! as Date)
}
}
But for whatever reason it gets ignored by sectionNameKeyPath. When I enter "date" in sectionNameKeyPath I get the same result as AllReadyHome mentioned above.
I even tried:
return (date?.description)!
It didn't yield any results.
Maybe someone can point me in the right direction...
Update 1:
That's what my fetchedResultsController look like:
let fetchRequest: NSFetchRequest<Time> = Time.fetchRequest()
fetchRequest.fetchBatchSize = 20
let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "sectionIdentifier", cacheName: "Time")
Result: No sections
When using date, which is an attribute of my entity:
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "date", cacheName: "Time")
It looks like this:
Update 2:
When using the sortDescriptor as suggested, I get a crash ("sectionIdentifier not in entity):
let fetchRequest: NSFetchRequest<Time> = Time.fetchRequest()
fetchRequest.fetchBatchSize = 20
let sortDescriptor1 = NSSortDescriptor(key: "sectionIdentifier", ascending: false)
let sortDescriptor2 = NSSortDescriptor(key: "date", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor1, sortDescriptor2]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "sectionIdentifier", cacheName: "Time")
Update 3:
I get the following warning when getting to the screen:
A section returned nil value for section name key path 'sectionIdentifier'. Objects will be placed in unnamed section
CoreData: error: (NSFetchedResultsController). A section returned nil value for section name key path 'sectionIdentifier'. Objects will be placed in unnamed section
use #objc for your sectionIdentifier
Sample:
#objc var sectionIdentifier: String {
get {
return ""
}
}
I am trying to retrieve the coordinates based on a specific mapname (in this case "farm") but somehow I can't get it to work.
My entities are "Map" and "Coordinates" with attributes "name", "latitude" and "longitude" respectively. Where have I made a mistake or what should I do differently to be able to access coordinates specific to only one mapname?
fileprivate var fetchedResultsControllerForCoordinateEntity: NSFetchedResultsController<Coordinate> = {
// Create Fetch Request
let fetchRequest: NSFetchRequest<Coordinate> = Coordinate.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "latitude", ascending: true)]
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "longitude", ascending: true)]
// Create Fetched Results Controller
let fetchedResultsControllerForCoordinateEntity = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultsControllerForCoordinateEntity
}()
fileprivate var fetchedResultsControllerForMapEntity: NSFetchedResultsController<Map> = {
// Create Fetch Request
let fetchRequest: NSFetchRequest<Map> = Map.fetchRequest()
// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
fetchRequest.predicate = NSPredicate(format:"name == %#", "farm")
// Create Fetched Results Controller
let fetchedResultsControllerForMapEntity = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultsControllerForMapEntity
}()
var newMap = NSEntityDescription.insertNewObject(forEntityName: "Map", into: managedObjectContext)
newMap.setValue(mapName, forKey: "name")
for member in self.markersArray {
var theCoordinate = NSEntityDescription.insertNewObject(forEntityName: "Coordinate", into: managedObjectContext)
theCoordinate.setValue(member.latitude, forKey: "latitude")
theCoordinate.setValue(member.longitude, forKey: "longitude")
theCoordinate.setValue(newMap, forKey: "newRelationship")
//newRelationship is a one-to-many relationship, as each map has several coordinates
}
I was using a lazy var in table view controller to get the data from CoreData. It was working good in swift2. But When I upgraded to Swift3, Its showing an error as below. Kindly help me to change for Swift3
Error : Cannot convert value of type 'Error' to specified type 'NSFetchedResultsController'
lazy var fetchedResultsController: NSfetchedResultsController =
{
let fetchRequest = NSFetchRequest(entityName: "EvtIvtTbl")
let sortDescriptor = NSSortDescriptor(key: "bym_kol", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
let fetchedResultsController = NSfetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: self.managedObjectContext,
sectionNameKeyPath: nil,
cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
In Swift 3.0, NSFetchedResultsController and NSFetchRequest want type parameters. Assuming that EvtIvtTbl is your NSManagedObject subclass, your code should look like this:
lazy var fetchedResultsController: NSFetchedResultsController<EvtIvtTbl> = {
let fetchRequest = NSFetchRequest<EvtIvtTbl>(entityName: "EvtIvtTbl")
let sortDescriptor = NSSortDescriptor(key: "bym_kol", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
let fetchedResultsController = NSFetchedResultsController<EvtIvtTbl>(fetchRequest: fetchRequest,
managedObjectContext: self.managedObjectContext,
sectionNameKeyPath: nil,
cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
I want to load more cells when user in the last cell. I use CoreData and I get data from it. I use this answer to do what I want, but it didn't work. What problem can be there? Also I have one more question. I don't know why working only first implementation.
First implementation
var fetchedResultsController: NSFetchedResultsController {
let moc = DatabaseController.sharedInstance.managedObjectContext
let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName("Coctail", inManagedObjectContext: moc)
fetchRequest.entity = entity
fetchRequest.fetchLimit = 10
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: moc,
sectionNameKeyPath: nil,
cacheName: nil)
aFetchedResultsController.delegate = self
do {
try aFetchedResultsController.performFetch()
} catch {
fatalError("\(error)")
}
return aFetchedResultsController
}
Second Implementation
func initilizeFetchedResultsController() {
let moc = DatabaseController.sharedInstance.managedObjectContext
let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName("Coctail", inManagedObjectContext: moc)
fetchRequest.entity = entity
fetchRequest.fetchLimit = 10
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: moc,
sectionNameKeyPath: nil,
cacheName: nil)
aFetchedResultsController.delegate = self
do {
try aFetchedResultsController.performFetch()
} catch {
fatalError("\(error)")
}
fetchedResultsController = aFetchedResultsController
}
Hard to tell why the first solution but the second doesn't without seeing the other code that would have used the fetchedResultsController or called initilizeFetchedResultsController().
The reason you're not getting more data is that you've hardcoded your fetchLimit to 10. No matter what you do, you'll only ever get 10 results.
You'll need to change the fetchLimit to get more data. Check out this answer -- it is similar to what you'll need to do: https://stackoverflow.com/a/6786931/5605379