views:

461

answers:

2

I tried to find the answer of my question on the internet, but I could not. I have a simple entity in Core data that has a Value attribute (that is integer) and a Date attribute. I want to define two methods in my .m file.

First method is the ADD method. It takes two arguments: an integer value (entered by user in UI) and a date (current date by default). and then insert a record into the entity based on the arguments.

Second method is like an increment method. It uses the Date as a key to find a record and then increment the integer value of that record.

I don't know how to write these methods. (assume that we have an Array Controller for the table in the xib file)

A: 

The second method you are looking for is not appropriate to add to the entity itself. It needs to be somewhere above the entities, most likely in your controller object.

The first method is as follows:

- (void)updateDate:(NSDate*)date andValue:(NSInteger)value
{
  [self setValue:date forKey:@"date"];
  [self setValue:[NSNumber numberWithInteger:value] forKey:@"value"];
}

This is fairly straight KVC (Key-Value Coding) and I highly recommend that you read Apple's docs on the subject.

For your other method, that should be in a controller, you need to perform a fetch to find the record.

- (id)findRecordForDate:(NSDate*)date inManagedObjectContext:(NSManagedObjectContext*)moc
{
  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  [request setEntity:[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:moc]];
  [request setPredicate:[NSPredicate predicateWithFormat:@"date == %@", date]];

  NSError *error = nil;
  NSArray *objects = [moc executeFetchRequest:request error:&error];
  NSAssert1(error == nil, @"Error fetching object: %@", [error localizedDescription]);

  return [objects lastObject];
}

- (void)incrementEntityWithDate:(NSDate*)date
{
  id entity = [self findRecordForDate:date inManagedObjectContext:[self managedObjectContext]];
  NSInteger value = [[entity valueForKey:@"value"] integerValue];
  value += 1;
  [entity setValue:[NSNumber numberWithInteger:value] forKey:@"value"];
}

This is also very straightforward Core Data access. I recommend you read up on how Core Data works.

As an aside, using a date as a unique is a very bad design.

UPDATE

Marcus, Thanks for the answer. It is really helpful. I am new to core data so I have a few questions to make things clearer.

The code for the first method sets two values, but it doesn't insert the new record into the table. How can I insert the newly created record into the table?

You need to read up on how Core Data works. Apple has great documentation on how Core Data works and if that fails you can buy my book. There is a TON of information about how to use Core Data on the internet.

Where should I put the first method? in my .m file?

This is basic Objective-C. If you are asking this question you need to step way back and learn the fundamentals of the language first.

You mentioned that I need to add the second method in the controller. But the controller is defined in the xib file. How can I add the second method to that?

The controller is never defined in the xib file. It is referenced in the xib file. Again, you need to go back to the beginning of how Objective-C works and learn the basics before you dive this deep.

Marcus S. Zarra
sfa
Hi Marcus, why don't you write the same book for iPhone? It would be best-selling book then ;)
sfa
Because everything in the book applies equally to the desktop and the iPhone. The library is the same.
Marcus S. Zarra
A: 

Marcus, Thanks for the answer. It is really helpful. I am new to core data so I have a few questions to make things clearer.

  1. The code for the first method sets two values, but it doesn't insert the new record into the table. How can I insert the newly created record into the table?

  2. Where should I put the first method? in my .m file?

  3. You mentioned that I need to add the second method in the controller. But the controller is defined in the xib file. How can I add the second method to that?

aminfar
It is appropriate to update your question instead of posting additional questions in an answer.
Marcus S. Zarra