ios - Getting Max ID from core data & sqlite -
i have table users , have bunch of columns in core data model. 1 of column user_id , trying highest user_id , add + 1 , pass next object inserted. followed apple developer guide , implemented suggested. here code trying execute.
issue : account_id value being returned being set weird number doesn't exists in database.
"account_id" = 139784529; supposed 1 since have saved in core data.
nsfetchrequest *request = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"users" inmanagedobjectcontext:self._managedobjectcontext]; [request setentity:entity]; // specify request should return dictionaries. [request setresulttype:nsdictionaryresulttype]; // create expression key path. nsexpression *keypathexpression = [nsexpression expressionforkeypath:@"account_id"]; // create expression represent function want apply nsexpression *expression = [nsexpression expressionforfunction:@"max:" arguments:@[keypathexpression]]; // create expression description using minexpression , returning date. nsexpressiondescription *expressiondescription = [[nsexpressiondescription alloc] init]; // name key used in dictionary return value. [expressiondescription setname:@"maxaccountid"]; [expressiondescription setexpression:expression]; [expressiondescription setexpressionresulttype:nsinteger32attributetype]; // example, nsdateattributetype // set request's properties fetch property represented expressions. [request setpropertiestofetch:@[expressiondescription]]; // execute fetch. nserror *error; nsarray *objects = [self._managedobjectcontext executefetchrequest:request error:&error]; if (objects == nil) { // handle error. } else { if ([objects count] > 0) { nextaccountid = [[[objects objectatindex:0] valueforkey:@"maxaccountid"] integervalue]; } } } @catch (nsexception *exception) { nslog(@"%@",[exception reason]); } return nextaccountid + 1;
hope faced issue before , fixed it.
thank you
thank taking @ question. thank comments , answers. after figuring out problem viewing sqlite database terminal, noticed table corrupted , giving junk values me. issue simple. have done fix it.
- deleted app iphone simulator.
- clean solution
- re compiled program. ran it, , bam.. worked.
just developer tip did not know accessing sqlite terminal.
figure out location sqlite. /user/username/library/application support/iphone simulator/6.1/xxxxxxxxxx/documents cd directory.
type sqlite3 command
attach "application.sqlite" db1
bam.. run commands.
if want @ databases running within - type .database
thank once again.
Comments
Post a Comment