core data - Swift Fetch Request Returning Empty Item In Results -


i have project need fetch request gets recent 'updated' date of core data entity. when examine results returned query seeing rather strange behaviour. in addition 'correct' result containing date results include empty dictionary item. happens every time, regardless of underlying data looks like. stranger, if turn on sql logging in xcode , execute logged query against sqllite db produces correct result no entries. i'm not quite sure i'm doing wrong here, appreciated.

the function builds , executes query:

func queryforcontactdate(context:nsmanagedobjectcontext) -> anyobject? {      var expressiondescriptions = [anyobject]();      let expressiondescription = nsexpressiondescription()      // name column     expressiondescription.name = "maxupdated"     // use expression specify aggregate action want take ,     // on column. in case max on update_at column     expressiondescription.expression = nsexpression(format: "@max.updated_at")     // specify return type expect     expressiondescription.expressionresulttype = .dateattributetype     // append description our array     expressiondescriptions.append(expressiondescription)      // build out our fetch request usual way     let request = nsfetchrequest(entityname: contact.entityname())      // specify want dictionaries returned     request.resulttype = .dictionaryresulttype      // hand off our expression descriptions propertiestofetch field.     request.propertiestofetch = expressiondescriptions      // our result going array of dictionaries.     var results:[[string:anyobject]]?      // perform fetch. using swfit 2, need do/try/catch     {         results = try context.executefetchrequest(request) as? [[string:anyobject]]     } catch _ {         // if fails, ensure array nil         results = nil     }      return results![0]; } 

if put breakpoint @ end , print out results produces:

printing description of results: ▿ optional([[:], ["maxupdated": 2015-12-30 20:05:31 +0000]])   ▿ : 2 elements     - [0] : 0 elements     ▿ [1] : 1 elements       ▿ [0] : 2 elements         - .0 : "maxupdated"         - .1 : 2015-12-30 20:05:31 +0000 

the traditional core data way max or min item query fetch limit of 1 , sort on key. in objective-c code:

+ (nsfetchrequest *) requestforstatuswithmaxid {      nsfetchrequest *request = [nsfetchrequest fetchrequestwithentityname: kamstatusentity];      nssortdescriptor *sd = [nssortdescriptor sortdescriptorwithkey: kamtwid ascending:no];      request.sortdescriptors = @[sd];     request.fetchlimit = 1;      return request;  } // +requestforstatuswithmaxid 

it quite simple modify above updated_at property.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -