I have following relationship between my entities.
I have 2 ViewControllers. 1 Where you add a playlist and 1 where you add a song. My question is then how can the song ViewController know which Playlist it needs to associate the song with and make create the relationship to that exact playlist?
PlaylistViewController.m (This is where i save my playlists)
-(void)saveTap:(id)sender{
Playlists *newManagedObject = (Playlists*)[NSEntityDescription insertNewObjectForEntityForName:@"Playlists" inManagedObjectContext:_managedObjectContext];
[newManagedObject setValue:self.textfield.text forKey:@"playlistName"];
// Save the context.
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self fetchDevices];
}
SongsViewController.m (This is where the songs is saved)
-(IBAction)songsDone:(id)sender{
AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSManagedObject *newManagedObject;
newManagedObject = [NSEntityDescription
insertNewObjectForEntityForName:@"Songs"
inManagedObjectContext:context];
NSDate *today= [NSDate date];
[newManagedObject setValue:video.author forKey:@"author"];
[newManagedObject setValue:video.videoid forKey:@"link"];
[newManagedObject setValue:video.title forKey:@"songName"];
[newManagedObject setValue:today forKey:@"created"];
// Save the context.
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
Song.h (Entity)
@class Playlists;
@interface Songs : NSManagedObject
@property (nonatomic, retain) NSString * author;
@property (nonatomic, retain) NSDate * created;
@property (nonatomic, retain) NSString * link;
@property (nonatomic, retain) NSString * songName;
@property (nonatomic, retain) NSSet *songs;
@end
@interface Songs (CoreDataGeneratedAccessors)
- (void)addSongsObject:(Playlists *)value;
- (void)removeSongsObject:(Playlists *)value;
- (void)addSongs:(NSSet *)values;
- (void)removeSongs:(NSSet *)values;
@end
Playlists.m (Entity)
@class Songs;
@interface Playlists : NSManagedObject
@property (nonatomic, retain) NSString * playlistName;
@property (nonatomic, retain) NSSet *list;
@end
@interface Playlists (CoreDataGeneratedAccessors)
- (void)addListObject:(Songs *)value;
- (void)removeListObject:(Songs *)value;
- (void)addList:(NSSet *)values;
- (void)removeList:(NSSet *)values;
@end