Ox Smith
Ox Smith

Reputation: 509

Trying to save elapsedTime to coreData

A core feature of my app is allowing users to track the amount of time spent at a location. However, I am running into (apparently) a serious error. I have two core-data entities: Location for saving a user's location - and TimeSpentStudying to save the time elapsed at that location (many-to-many relationship). I am attaching a screenshot to make it clearer:

https://i.sstatic.net/nIOAF.png

Here is my TimeSpentStudying class:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Location;

@interface TimeSpentStudying : NSManagedObject

@property (nonatomic, retain) NSString * libraryNameText;
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSNumber * timeSpent;
@property (nonatomic, retain) Location *info;

@end

Here is where I pass the Location entity to the viewController that will be keeping track of the elapsed time - LibraryTrackTimeViewController :

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if (distance < 500) {
  if ([segue.identifier isEqualToString:@"TrackLibraryTimes"]) {
    UINavigationController *navigationController = segue.destinationViewController;

    LibraryTrackTimeViewController *controller = (LibraryTrackTimeViewController *)navigationController.topViewController;
    controller.managedObjectContext = self.managedObjectContext;
    controller.libraryNameText = [[[mapView selectedAnnotations] objectAtIndex:0] title];
  }
  }
}

When I start the timer - receive this error:

<TimeSpentStudying: 0x1e441130> (entity: TimeSpentStudying; id: 0x1d5a4260 <x-coredata:///TimeSpentStudying/t3B884C6F-8210-4B9E-A716-23DEC24291482> ; data: {
    date = nil;
    info = nil;
    libraryNameText = nil;
    timeSpent = 0;
})
2013-01-29 17:46:25.885 BooksonBooksonBooks[18856:907] -[TimeSpentStudying coordinate]: unrecognized selector sent to instance 0x1e441130
2013-01-29 17:46:25.892 BooksonBooksonBooks[18856:907] CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  -[TimeSpentStudying coordinate]: unrecognized selector sent to instance 0x1e441130 with userInfo (null)
2013-01-29 17:46:25.899 BooksonBooksonBooks[18856:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TimeSpentStudying coordinate]: unrecognized selector sent to instance 0x1e441130'

Here is the method I believe is causing the error:

-(IBAction)startTimer:(id)sender
{
  startTime = [[NSDate alloc] init];

  elapsedTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];

  TimeSpentStudying *timeSpentStudying = [NSEntityDescription insertNewObjectForEntityForName:@"TimeSpentStudying" inManagedObjectContext:self.managedObjectContext];

  timeSpentStudying.timeSpent = [NSNumber numberWithDouble:totalTimeSpentStudying];

I don't quite understand the error. Do I have to create whole new managedObject context in my LibraryTrackTimeViewController specifically for TimeSpentStudying ? I want each Location object to have multiple TimeSpentStudying timeIntervals saved (date, timeSpent, etc). I hope I am being clear enough. If you need to see more of LibraryTrackTimeViewController - just let me know, thanks!

Upvotes: 0

Views: 135

Answers (1)

jsd
jsd

Reputation: 7693

The error is here:

BooksonBooksonBooks[18856:907] -[TimeSpentStudying coordinate]: unrecognized selector sent to instance 0x1e441130

You're calling "coordinate" on an object of class TimeSpentStudying, which doesn't implement that method, and that's causing the app to blow up.

Since I don't see coordinate in any of the code you posted, and you said your other entity type was location, which presumably contains a coordinate, you're probably passing a location where you meant to pass a timespentstudying. Are you using ARC?

Upvotes: 1

Related Questions