i think i am not bad at creating code to DO STUFF. But i am terrible at storing my variables and persistent data. I am using ARC and storyboards (thinking about ditching the storyboards since they seem to do stuff under hood which i dont see).
I ask for help. I was unable to grasp the concept how to do what i want from the sources i found untill now.
My idea is: User is presented with viewController of type tableviewcontroller that shows him a string (Stock quote name) for each row. The strings need to be stored persistently i presume in coredata. The application allows the user to add strings in a secondviewcontroller. These need to be saved persistently along the first data too. The application then needs to get some webbased data and show them alongside the strings (Stock prices). When the app quits, it can delete the webbased data and just keep the strings (Stock quote names).
I ask for help with with either.
The app shows no warnings and compiles without any problem.
Then it crashes right after NSLog(@"2"); in viewDidLoad
in the code below, with * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Stock''
Appdelegate.h
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
Appdelegate.m
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (void)prvniSpusteni
{
NSLog(@"Firt run of app, example Stocks added.");
NSManagedObjectContext *context = [self managedObjectContext];
// Creating, Apple, Google, Bekrshire B
Stock *akcie1 = [NSEntityDescription insertNewObjectForEntityForName:@"Stock" inManagedObjectContext:context];
akcie1.ticker = @"AAPL";
akcie1.index = [NSNumber numberWithInt:0];
Stock *akcie2 = [NSEntityDescription insertNewObjectForEntityForName:@"Stock" inManagedObjectContext:context];
akcie2.ticker = @"HIMX";
akcie2.index = [NSNumber numberWithInt:1];
Stock *akcie3 = [NSEntityDescription insertNewObjectForEntityForName:@"Stock" inManagedObjectContext:context];
akcie3.ticker = @"BRK-B";
akcie3.index = [NSNumber numberWithInt:2];
NSError *error;
if (![context save:&error]) {
NSLog(@"Error: %@", [error localizedDescription]);
}
[self aktualizaceDatTrhu]; // method to get data from web declared in Appdelegate aswell.
}
ViewController.h
@property (nonatomic, strong) NSManagedObjectContext* managedObjectContext;
@property (nonatomic, strong) NSArray *ulozeneAkcie;
ViewController.m
@interface ViewController ()
@end
@implementation ViewController
@synthesize managedObjectContext;
@synthesize ulozeneAkcie;
- (void)viewDidLoad
{
[super viewDidLoad];
NSManagedObjectContext *context = managedObjectContext;
**NSLog(@"2");**
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Stock" inManagedObjectContext:context];
NSLog(@"3");
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSLog(@"4");
[request setEntity:entityDescription];
NSLog(@"5");
// Nastav trideni do arraye
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"index" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];
NSLog(@"6");
NSError *error;
ulozeneAkcie = [managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"7");
}
Now i think the problem is that the viewcontroller somehow fails to access the managedobjectcontext. But i thought coredata will be treated as global and could be accessed from any part of the app like a box of lego blocks.
My deepest thanks to anyone who reads as far as here and even deeper thanks to anyone who offers insight.
If anyone feels like it i would simply love to get an example of where to put which property, where to put which synthesize and hooks so that this should actually work.
P.S.: i found some "similar" questions online and those spoke about passing the MOC to the viewController, but since my app is made with storyboards i miss lot of hooks and outlets and adding them doesnt solve anything..
context
(the second parameter) isnil
. TheNSManagedObjectContext
and other core data components are not "global". Often, core data is initialized/managed through the app delegate. Suggest reading developer.apple.com/library/mac/#documentation/cocoa/Conceptual/…, and going through a core data tutorial such as raywenderlich.com/934/… – bobnoble