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.

  1. deleted app iphone simulator.
  2. clean solution
  3. re compiled program. ran it, , bam.. worked.

just developer tip did not know accessing sqlite terminal.

  1. figure out location sqlite. /user/username/library/application support/iphone simulator/6.1/xxxxxxxxxx/documents cd directory.

  2. type sqlite3 command

  3. attach "application.sqlite" db1

  4. bam.. run commands.

  5. if want @ databases running within - type .database

thank once again.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -