/coredata-objc

Coredata Objc

Primary LanguageObjective-C

coredata-objc

Coredata Objc

Objective c Coredata example with create, edit, read, delete methods

  1. Create a new project in xcode
  2. Add Coredata.framework in Linked Frameworks and Libraries project
  3. Copy and paste coredata folder that is inside CoredataExample > CoredataExample
  4. Create a new data model in cmd+n > core data > next. Put a name that will be used and save
  5. Create your entity, in attributes insert your attributes
  6. In folder coredata, get the file Item and change this method with your Entity's name
  • (NSString*)entityName { return @"Entity"; }
  1. Customize your attributes in Item.h
  2. Put your attributes in this method:
  • (BOOL)insertItemWithkey:(NSString*)key withValue:(NSString *) value inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
  1. In your Controller.h insert: #import "Item.h" #import "PersistentStack.h" @class PersistentStack;

  2. In your Controller.m put:

@interface ViewController () @property (nonatomic, strong) PersistentStack* persistentStack; @property (nonatomic,strong) NSManagedObjectContext* managedObjectContext; @end

  1. In methods (NSURL*)storeURL and (NSURL*)modelURL, change to your model name

  2. Finally, use the method that u want

[Item insertItemWithkey:key withValue:value inManagedObjectContext:self.managedObjectContext ];

[Item getAllItemsWithMOC: self.managedObjectContext];

Item *item = [Item getItemByKey:@"my_key_2" withOMC: self.managedObjectContext];

[Item deleteAll:self.managedObjectContext];

[Item removeItemWithKey:@"my_key_2" withOMC: self.managedObjectContext];


Tips:

  • Sometimes when you create the model file, it comes with code generation swift selected. So, you need to change it, otherwise will occur some errors.

To change this click on your model > click show the file inspector (in right panel) > Code generation > Lnaguage > Objective C