Developer
Developer

Reputation: 4321

Core Data and NSManagedObject

I have next problem:

I have added Model.xcdatamodeld

In it i have 1 entity - Device with 3 attributes: name, version, company;

In my DetailController i have added this:

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
    context = [delegate managedObjectContext];
    }
    return context;
}

And on Button click i want to save data:

- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];

//Create a new managed object
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
/*


[newDevice setValue:self.nameTextField.text forKey:@"name"];
[newDevice setValue:self.versionTextField.text forKey:@"version"];
[newDevice setValue:self.companyTextField.text forKey:@"company"];

NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

[self dismissViewControllerAnimated:YES completion:nil];
 */
}

And it throws error on NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];

What did i do wrong? Maybe if i add DataModel manually i should wire it up somewhere?

And second question is theoretical:

I read tutorial about developing in XCODE and i currently read about CoreData and it says:

Quote: "Fetch device information from persistent store (i.e. SQLite database) and populate the data into Table View Controller"

Does it mean that Core data is the SQLite db?

ERROR:

Unknown type name NSManagedObject; did you mean NSManagedObjectModel?

Edited my code:

Added CoreData.framework

Added this to AppDelegate.h

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

Added this to AppDelegate.m

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

Still error...

Upvotes: 0

Views: 941

Answers (2)

sunsunai
sunsunai

Reputation: 161

I don't know my answer still helpful or not but this error show up because you didn't import CoreData.framework also import CoreData in your class you want to use it.

import "CoreData/CoreData.h" in your class (.h)

Upvotes: 2

rakmoh
rakmoh

Reputation: 2963

Here is the code that you need to add to your AppDelegate.m

- (void)saveContext
{
  NSError *error = nil;
  NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
  if (managedObjectContext != nil)
  {
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
    {
        //TODO Proper error handling
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    } 
  }
}

#pragma mark - Core Data stack

/**
Returns the managed object context for the application.
If the context doesn't already exist, it is created and bound to the persistent store       coordinator for the application.
*/
- (NSManagedObjectContext *)managedObjectContext
{
  if (__managedObjectContext != nil)
  {
    return __managedObjectContext;
  }

  NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  if (coordinator != nil)
  {
    __managedObjectContext = [[NSManagedObjectContext alloc] init];
    [__managedObjectContext setPersistentStoreCoordinator:coordinator];
  }
  return __managedObjectContext;
}

/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created from the application's model.
*/
- (NSManagedObjectModel *)managedObjectModel
{
  if (__managedObjectModel != nil)
  {
    return __managedObjectModel;
  }
  NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyAppTesting" withExtension:@"momd"];
  __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
  return __managedObjectModel;
}

/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
  if (__persistentStoreCoordinator != nil)
  {
    return __persistentStoreCoordinator;
  }

  NSURL *storeURL = [[self applicationDocumentsDirectory]   URLByAppendingPathComponent:@"MyAppTesting.sqlite"];

  NSError *error = nil;
  __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
  {
    //TODO Error handling 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
  }    

  return __persistentStoreCoordinator;
}

Upvotes: 0

Related Questions