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
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.
Just a developer tip for those who did not know about accessing sqlite from terminal.
Figure out the location to your sqlite. Usually /User/UserName/Library/Application Support/iPhone Simulator/6.1/xxxxxxxxxx/Documents cd to this directory.
type sqlite3 command
attach "Application.sqlite" as db1
bam.. run your commands.
If you want to look at the databases running within - type .database
Thank you all once again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With