ios - Difficulty configuring NSFetchedResultsController in Swift 3 -
i'm refactoring existing project swift 2 swift 3. has been straightforward until got refactoring core data. i'm able create managed objects , persist them in managedobjectcontext
, i'm having difficulty getting nsfetchedresultscontroller
work. took @ this post, it's not getting me across finish line.
after importing records json, verify there objects in managedobjectcontext
following code:
func recordcount() -> int { let context = (uiapplication.shared.delegate as! appdelegate).persistentcontainer.viewcontext let fetchrequest: nsfetchrequest<nsfetchrequestresult> = nsfetchrequest(entityname: "myentity") let count = try! context.count(for: fetchrequest) return count }
when create fetchedresultscontroller
, i'm running trouble. code doesn't crash, doesn't return nsmanagedobjects
despite there being objects match search.
here's how i'm creating nsfetchedresultscontroller
in uiviewcontroller
.
class myviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource, nsfetchedresultscontrollerdelegate { // set on prior viewcontroller before segue. // i've verified it's not nil var selectedequipmentstring: string? let managedobjectcontext = (uiapplication.shared.delegate as! appdelegate).persistentcontainer.viewcontext lazy var fetchedresultscontroller: nsfetchedresultscontroller<myentity> = { // i've tried altering syntax of fetchrequest // let fetchrequest: nsfetchrequest<myentity> = myentity.fetchrequest() let fetchrequest: nsfetchrequest<nsfetchrequestresult> = nsfetchrequest(entityname: "myentity") let sortdescriptor = nssortdescriptor(key: "generalarea", ascending: true) fetchrequest.sortdescriptors = [sortdescriptor] fetchrequest.predicate = nspredicate(format: "equipmentdescription == %@", self.selectedequipmentstring!) let frc: nsfetchedresultscontroller<myentity> = nsfetchedresultscontroller(fetchrequest: fetchrequest as! nsfetchrequest<myentity>, managedobjectcontext: self.managedobjectcontext, sectionnamekeypath: "generalarea", cachename: nil) frc.delegate = self return frc }() // mark: - view lifecycle methods (abbreviated) override func viewdidload() { super.viewdidload() // i've tried moving call viewwillappear , viewdidappear without success fetchobjectsfrommanagedobjectcontext() } // mark: - core data methods (abbreviated) func fetchobjectsfrommanagedobjectcontext() { { try fetchedresultscontroller.performfetch() } catch { print("error: \(error)") return } print ("there \(fetchedresultscontroller.fetchedobjects!.count) returned fetchobjectsfrommanagedobjectcontext") } }
this code doesn't crash, doesn't return records fetchrequest. able force crash typo in predicate, without typo there no objects returned despite objects match predicate.
i welcome suggestions re: mistake is. rest assured knowing startlingly silly oversight on part. thank reading.
your nsfetchrequest should have type nsfetchrequest<myentity>
, specify nsfetchrequest<nsfetchrequestresult>
. try changing , let me know if helps or not
Comments
Post a Comment