kusumoto_teruya kusumoto_teruya - 5 months ago 72
Objective-C Question

Insert data with MagicalRecord

I am using

CoreData
with
MagicalRecord
.

I'd like to insert
Data
in following code,
but to insert data become an error with a message
Cocoa error 133000
.

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"class_schedule.sqlite"];

return YES;
}


ViewController.m

- (void)saveData
{
Data *data = [Data MR_createEntity];
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
Data *localData = [data MR_inContext:localContext];
localData.title = textField.text;
} completion:^(BOOL success, NSError *error) {
}];
}


Data.h

@interface Data : NSManagedObject

@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * title;

@end


Can you tell me how to insert record with Magical Record?

Answer

Error:

  • Cocoa error 133000 is:

NSManagedObjectReferentialIntegrityError = 133000, // attempt to fire a fault pointing to an object that does not exist (we can see the store, we can't see the object)

  • (Taken from this SO question). Basically you are doing something with a NSManagedObject that doesn't exist.

Inserting data:

  • In terms of how to insert data using magical record take a look at this tutorial which will probably explain it much better than I can.

My advice:

  • Use Core Data straight up. It's quite a steep learning curve, but very quickly becomes intuitive and easy to use. It will also stand you in good stead if you know how it all works rather than relying on a third party.

  • If you're interested in how it works at a more fundamental level, take a look at SQLite. I wouldn't necessarily recommend using it as it is a C library but it will help you get a deeper understanding.

Comments