PDA

View Full Version : update my core data database




DrakeRamoray
Aug 8, 2012, 02:23 AM
Hi everyone.

I must to copy datas from my external database to core data and I have a little problem.
I use Json to communicate with iOS and it's work pretty well, each package of datas have an ID number.
I just want to save this datas in the iphone if the ID number is not saved yet on my IOS database.
How can I do?

Thank you



forum user
Aug 8, 2012, 01:02 PM
You might want to look at NSExpression. Something similar to:

NSExpression * expressionDescriptionCount =
[NSExpression expressionForFunction:@"count:"
arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"IDnumber"]]];
[fetchRequest setResultType:NSDictionaryResultType];
NSArray * results = [theContext executeFetchRequest:fetchRequest error:&error];

After the fetch went ok "results" will contain a dictionary.
If the value of the count key inside the dictionary is zero then your "id Number" is not yet present. Otherwise it will be the the nubmer of occurrence.

Ensure you match the string @"IDnumber" in expressionForKeyPath:@"IDnumber" with the correct spelling of the attribute.

Duncan C
Aug 8, 2012, 03:19 PM
You might want to look at NSExpression. Something similar to:

NSExpression * expressionDescriptionCount =
[NSExpression expressionForFunction:@"count:"
arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"IDnumber"]]];
[fetchRequest setResultType:NSDictionaryResultType];
NSArray * results = [theContext executeFetchRequest:fetchRequest error:&error];

After the fetch went ok "results" will contain a dictionary.
If the value of the count key inside the dictionary is zero then your "id Number" is not yet present. Otherwise it will be the the nubmer of occurrence.

Ensure you match the string @"IDnumber" in expressionForKeyPath:@"IDnumber" with the correct spelling of the attribute.


I don't understand the code you posted. You create an NSExpression object, and then you issue a fetch request that doesn't use the expression at all. Aren't you missing code that would add the expression to a predicate, and add the predicate to the fetch request? As written, the code uses an existing fetch request that has nothing to do with the expression object you created.