core-data

Is there an tutorial that shows how attribute validation works in Core Data?

I'd like to play around with attribute value validation, but the documentation is pretty empty on this. Maybe there's an good article or tutorial out there? ...

How does a custom accessor method implementation in Core Data look like?

The documentation is pretty confusing on this one: The implementation of accessor methods you write for subclasses of NSManagedObject is typically different from those you write for other classes. If you do not provide custom instance variables, you retrieve property values from and save values into the internal st...

So where is this calling super?

From the Core Data docs: Inheritance If you have two subclasses of NSManagedObject where the parent class implements a dynamic property and its subclass (the grandchild of NSManagedObject) overrides the methods for the property, those overrides cannot call super. @interface Parent : NSManagedObject @property(nonatomic, ...

Couldn't I just pass an copied string to an Core Data property?

The docs say: The default implementation does not copy attribute values. If the attribute value may be mutable and implements the NSCopying protocol (as is the case with NSString, for example), you can copy the value in a custom accessor to help preserve encapsulation (for example, in the case where an instance of N...

how to make an persistet attribute with an scalar value like CGFloat or CGRect, in Core Data?

Yeah, the documentation has an example: @interface Circle : NSManagedObject { CGFloat radius; } @property CGFloat radius; @end @implementation Circle - (CGFloat)radius { [self willAccessValueForKey:@"radius"]; float f = radius; [self didAccessValueForKey:@"radius"]; return f; } - (void)setRadius:(CGFloat)newRadius...

What's -mutableSetValueForKey: returning, actually? Is that something special?

From the docs: You usually access to-many relationships using mutableSetValueForKey:, which returns a proxy object that both mutates the relationship and sends appropriate key-value observing notifications for you. So this returns an "intelligent" NSMutableSet which automatically lets the context delete objects when the...

What's the fastest and easiest way to create a new managed object?

When I have a subclass of NSManagedObject, I could do this: Friend *newFriend = (Friend*)[[NSManagedObject alloc] initWithEntity:@"Friend" insertIntoManagedObjectContext:moc]; But since I have a subclass, couldn't I simply instantiate it another way so that I don't have to tell it what Entit...

When does validation happen in Core Data?

From the docs: If you make changes to managed objects associated with a given context, those changes remain local to that context until you commit the changes by sending the context a save: message. At that point—provided that there are no validation errors—the changes are committed to the store. So does that essentia...

What's does "cardinality of an relationship" mean in Core Data?

From the docs: If all of a managed object's relationship delete rules are Nullify, then for that object at least there is no additional work to do (you may have to consider other objects that were at the destination of the relationship—if the inverse relationship was either mandatory or had a lower limit on cardinalit...

What exactly is the Nullify delete rule doing?

Does that mean that if I delete an managed object which has references (relationship) to some others, the relationships are removed to those others? Example: objectA references objectB and objectC. objectA gets deleted, it's relationship to objectB and objectC is set to the Nullify rule. What happens in detail? ...

Binding to a relations property in Core Data

I'm new in Core Data, and i got a problem i can't get my head around how to do "the right way" I'll try and examplify my problem. I got a entity Car. And a list of all the cars in my program. The cars have some attributes, but they are not predefined. So for each car i want to be able to define some properties. Therefore i have define...

How to Determine if a Table Contains Any Records with Core Data?

I haven't seen any other questions quite like this on here, but I'm hoping someone has some insight. I'm just starting to learn Core Data. Basically, I have two methods and I want to choose which one to call with an if/else statement based on whether or not the "Contacts" table contains any records. Is there a way using core data to che...

When my relationship delete rule is set to Nullify, will the inverse relationship also be deleted?

Example: I have a Department and an Employee. Department has a Nullify relationship to many Employees, and an Employee has an inverse relationship to one Department. Now I remove the relationship to an Employee. So lets say the Marketing Department doesn't link anymore to Joe Stevens. 1) Does Joe Stevens still link to the Marketing De...

Detecting a Lightweight Core Data Migration

I'm using Core Data's automatic lightweight migration successfully. However, when a particular entity gets created during a migration, I'd like to populate it with some data. Of course I could check if the entity is empty every time the application starts, but this seems inefficient when Core Data has a migration framework. Is it possib...

EXC_MEMORY_ACCESS when trying to delete from Core Data ($cash solution)

I have an application that downloads an xml file, parses the file, and creates core data objects while doing so. In the parse code I have a function called 'emptydatacontext' that removes all items from Core Data before creating replacements items from the xml data. This method looks like this: -(void) emptyDataContext { NSFetchRequest...

Can get members, but not count of NSMutableArray

I'm filling an NSMutableArray from a CoreData call. I can get the first object, but when I try to get the count, the app crashes with Program received signal: “EXC_BAD_ACCESS”. How can I get the count? Here's the relevant code - I've put a comment on the line where it crashes. - (void)viewDidLoad { [super viewDidLoad]; managedObjec...

What should I do when the managed object context fails to save?

Example: I have an Cat entity with an catAge attribute. In the data modeler, I configured catAge as int with a max of 100. Then I do this: [newManagedObject setValue:[NSNumber numberWithInt:125] forKey:@"catAge"]; // Save the context. NSError *error = nil; if (![context save:&error]) { NSLog(@"Unresolved error %@, %@", error, [erro...

So does Apple recommend to not use predicates and sort descriptors in an NSFetchRequest?

From the docs: To summarize, though, if you execute a fetch directly, you should typically not add Objective-C-based predicates or sort descriptors to the fetch request. Instead you should apply these to the results of the fetch. If you use an array controller, you may need to subclass NSArrayController so you can hav...

How to do an fetch request with expressions like this on the iPhone?

The documentation has an example on how to retrieve simple values only, rather than managed objects. This remembers a lot SQL using aliases and functions to only retrieve calculated values. So, actually pretty geeky stuff. To get the minimum date from a bunch of records, this is used on the mac: NSFetchRequest *request = [[NSFetchReques...

Does this collection operator method also work on the iPhone?

NSNumber *salaryOverhead = [anEmployee valueForKeyPath:@"[email protected]"]; ...