swift - Why is my fetchRequest returning nil? -


my fetch request returning nil entity think i've put data , after hours , hours of debugging, haven't been able figure out. (disclaimer: i've looked @ countless threads here on talk fetch requests , unwrapping options every 1 found how deal unwrapping error. i'm asking figuring out why fetch request isn't returning record when believe should.)

in view controller i'm going display data, have in viewwillappear:

class liftlogtableviewcontroller: uitableviewcontroller {      var managedobjectcontext: nsmanagedobjectcontext!     var liftevents = [liftevent]()       override func viewwillappear(animated: bool) {         super.viewwillappear(animated)      let fetchrequest = nsfetchrequest(entityname: "liftevent")      {       if let results = try managedobjectcontext.executefetchrequest(fetchrequest) as? [liftevent] {         liftevents = results  <---- nil error happening here          }     }  catch {       fatalerror("error fetching data!")     }   } 

but believe there's record in there because in appdelegate.adddata() i've done this:

    // create lift event in liftevent     let liftevent = liftevent(entity: liftevententity, insertintomanagedobjectcontext: managedobjectcontext)      let datestr = "05-27-2016"     let dateformatter = nsdateformatter()     dateformatter.dateformat = "mm-dd-yyyy"     let date: nsdate = dateformatter.datefromstring(datestr)!      liftevent.date = date     liftevent.lifteventuid = 1     liftevent.liftuid = 1     liftevent.formulauid = 1     liftevent.maxamount = 250      print("added lift id: \(liftevent.lifteventuid) lift id: \(liftevent.liftuid) weight: \(liftevent.maxamount) formula: \(liftevent.formulauid) ")      savecontext()    }  func savecontext () {     if managedobjectcontext.haschanges {       {         try managedobjectcontext.save()       } catch {         // replace implementation code handle error appropriately.         // abort() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development.         let nserror = error nserror         nslog("unresolved error \(nserror), \(nserror.userinfo)")         abort()       }     }   } 

i'm learning coredata @ point have assume there's basic i'm missing.

thanks in advance.

assuming you're using standard core data template need assign value managedobjectcontext property in liftlogtableviewcontroller class.

for example in viewdidload

override func viewdidload() {   super.viewdidload()   let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate   managedobjectcontext = appdelegate.managedobjectcontext } 

however recommended way pass context via segue.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -