I have been reading through the core data documentation and feel I am still missing something.
I do not quite understand how you insert objects into a relationship of another object.
For example the following two Entities are in my model
flightDepartureBoard
name:
from_airport:
to_airport:
current_flights: (this is a one to many relationship of flight detail entities)
flight_details
arrive
depart
name
So my data contains a list of different departure boards for a few airports. Each departure board then contains a number of flight_details containing info on the current arrivals and departures for that airport.
My current understanding is to insert the flight details for a specific departure board, I must get the managedObject for the board, then create a new managed object for each flight and set its values as appropriate then create an NSSet conatining the flight managed objects and set the depatureboards managedObject current_flights (the relationship) to the just created NSSet. Is this correct?
What if I want to add new entries? I assume I do not need to fetch the entire set first?
Thanks for any help..
Although I just realised I could set the relationship to the current object on the flightDetails object instead..
To update a specific object you need to set up a NSFetchRequest . This class is equivalent to a SELECT statetement in SQL language. The array results contains all the managed objects contained within the sqlite file. If you want to grab a specific object (or more objects) you need to use a predicate with that request.
The first step in working with Core Data is to create a data model file to define the structure of your app's objects, including their object types, properties, and relationships. You can add a Core Data model file to your Xcode project when you create the project, or you can add it to an existing project.
In Core Data, faults are placeholders, or “unrealized objects”. They are small objects which refer to other NSManagedObjects, which are fetched into memory only as needed. This faulting mechanism is designed to enhance performance and reduce memory use.
The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.
There are a few options:
1. You can use add<Key>Object:
on a NSManagedObject:
NSManagedObject *flightBoard = [NSEntityDescription insertNewObjectForEntityForName:@"FlightDepartureBoard" inManagedObjectContext:self.managedObjectContext];
NSManagedObject *details = [NSEntityDescription insertNewObjectForEntityForName:@"Flight_Details" inManagedObjectContext:self.managedObjectContext];
[flightBoard addCurrent_flightsObject:details];
Although you will get a compiler warning unless you define the accessors in a category:
@interface NSManagedObject (Current_flightsAccessors)
- (void)addCurrent_flightsObject:(NSManagedObject *)value;
- (void)removeCurrent_flightsObject:(NSManagedObject *)value;
- (void)addCurrent_flights:(NSSet *)value;
- (void)removeCurrent_flights:(NSSet *)value;
@end
2. Subclass. You can generate class files from your model which will include accessors for the to-many relationships:
FlightDepartureBoard.h:
#import <CoreData/CoreData.h>
@class Flight_Details;
@interface FlightDepartureBoard : NSManagedObject
{
}
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * from_airport;
@property (nonatomic, retain) NSString * to_airport;
@interface FlightDepartureBoard (CoreDataGeneratedAccessors)
- (void)addCurrent_flightsObject:(Flight_Details *)value;
- (void)removeCurrent_flightsObject:(Flight_Details *)value;
- (void)addCurrent_flights:(NSSet *)value;
- (void)removeCurrent_flights:(NSSet *)value;
@end
FlightDepartureBoard.m:
#import "FlightDepartureBoard.h"
#import "Flight_Details.h"
@implementation FlightDepartureBoard
@dynamic name;
@dynamic from_airport;
@dynamic to_airport;
@dynamic current_flights;
@end
3. You can get the mutable set and modify it using mutableSetValueForKey:. For example:
NSManagedObject *flightBoard = [NSEntityDescription insertNewObjectForEntityForName:@"FlightDepartureBoard" inManagedObjectContext:self.managedObjectContext];
NSManagedObject *details = [NSEntityDescription insertNewObjectForEntityForName:@"Flight_Details" inManagedObjectContext:self.managedObjectContext];
NSMutableSet *flights = [flightBoard mutableSetValueForKey:@"current_flights"];
[flights addObject:details];
For more information, check out Managed Object Accessor Methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With