Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Max ID from core data & sqlite

Tags:

ios

max

core-data

I have a table Users and have bunch of columns in the core data model. One of the column is user_id and I am trying to get the highest user_id and add + 1 to it and pass it to the next object that will be inserted. I followed apple developer guide and implemented as suggested. Here is the code that I am trying to execute.

Issue is : My account_id value that is being returned is being set to some weird number which even doesn't exists in the database.

"account_id" = 139784529; It is supposed to be 1 since I only have saved in core data.

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Users" inManagedObjectContext:self._managedObjectContext];
[request setEntity:entity];

// Specify that the request should return dictionaries.
[request setResultType:NSDictionaryResultType];

// Create an expression for the key path.
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"account_id"];

// Create an expression to represent the function you want to apply
NSExpression *expression = [NSExpression expressionForFunction:@"max:"
                                                     arguments:@[keyPathExpression]];

// Create an expression description using the minExpression and returning a date.
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

// The name is the key that will be used in the dictionary for the return value.
[expressionDescription setName:@"maxAccountId"];
[expressionDescription setExpression:expression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType]; // For example, NSDateAttributeType

// Set the request's properties to fetch just the property represented by the expressions.
[request setPropertiesToFetch:@[expressionDescription]];

// Execute the fetch.
NSError *error;
NSArray *objects = [self._managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
    // Handle the error.
}
else {
    if ([objects count] > 0) {
        nextAccountId = [[[objects objectAtIndex:0] valueForKey:@"maxAccountId"] integerValue];
    }
}
}

@catch (NSException *exception) {
    NSLog(@"%@",[exception reason]);
}

return nextAccountId + 1;

Hope someone faced this issue before and fixed it.

Thank you

like image 456
Harish Avatar asked Oct 05 '22 04:10

Harish


1 Answers

Thank you all for taking a look at the question. Also thank you for the comments and answers. After figuring out the problem viewing sqlite database from the terminal, I noticed the table was corrupted and it was giving all junk values back to me. The issue was really simple. This is what I have done to fix it.

  1. Deleted the app from the iPhone Simulator.
  2. Clean solution
  3. re compiled the program. Ran it, and bam.. it worked.

Just a developer tip for those who did not know about accessing sqlite from terminal.

  1. Figure out the location to your sqlite. Usually /User/UserName/Library/Application Support/iPhone Simulator/6.1/xxxxxxxxxx/Documents cd to this directory.

  2. type sqlite3 command

  3. attach "Application.sqlite" as db1

  4. bam.. run your commands.

  5. If you want to look at the databases running within - type .database

Thank you all once again.

like image 118
Harish Avatar answered Oct 13 '22 11:10

Harish