0

I have following relationship between my entities. enter image description here

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
user3195388
  • 129
  • 1
  • 10
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Grzegorz Krukowski Jan 16 '14 at 11:18
  • i'm not asking how to pass data between view controllers. I'm asking how im going to associate the song with a playlist without having an auto-increment id like in SQL, by using relationships. – user3195388 Jan 16 '14 at 11:26

1 Answers1

0

You need to offer the user some way to choose the playlist that should be associated with the song(s). 2 general options:

  1. Select a playlist to see and edit the songs it contains (so the playlist is passed from the first controller to the second).
  2. Display song details and have a button to show a playlist picker (could be a picker view or a table view). This uses delegation to return the playlist to the song controller.

Once you have the playlist and the song managed objects, use:

[playlist addSongsObject:song];

before saving the context (which will update both objects relationships as they are inverse).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I already have these things. The problem is how do i associate the songs with a playlist. i've tried by associating the songs by the playlist ObjectID or indexpath.row, but they will change on deleting a row and restarting the app. – user3195388 Jan 16 '14 at 11:48
  • and how will the song here know which one from the other ViewController is selected. do i need to pass something between the to viewcontrollers? – user3195388 Jan 16 '14 at 11:55
  • Yes, you need to pass the playlist object to the song view controller. You need to have both of the managed object instances to configure the relationship. – Wain Jan 16 '14 at 11:57
  • I'm passing data via Singleton optionsSingle = [rowNumber singleObj]; optionsSingle = [devices objectAtIndex:indexPath.row]; Is this what you mean by passing the managed object? (devices is the NsMutableArray where all the playlists is) – user3195388 Jan 16 '14 at 12:40
  • Not clear what that is. Your controller should be able to access an instance of `Playlists` and of `Songs` so it can connect them. I guess 2 different controllers will be used to select them. You need to pass the first instance selected to the second controller. – Wain Jan 16 '14 at 14:29