如何在现有实体的核心数据关系属性中添加新对象

时间:2019-03-22 09:22:28

标签: ios objective-c core-data

这就是我在做什么

我创建了一个名为MoviesPlayList的实体,其中包含 here movieList is my relationship attribute

this is my another entity which is in relationship with first entity

我需要向现有的关系属性中添加新数据。

这是我的代码

NSManagedObject *updateDevice = [self.playlistDataArray objectAtIndex:indexPath.row];

       NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"MoviesInsidePlaylist" inManagedObjectContext:[self managedObjectContext]];

       NSManagedObject *insideMoviesManagedObject = [[NSManagedObject alloc]initWithEntity:entityDescription insertIntoManagedObjectContext:[self managedObjectContext]];


        //Featch Movie Results
        NSManagedObjectContext *managedobjectcontext = [self managedObjectContext];
        NSFetchRequest *fetchrequest = [[NSFetchRequest alloc] initWithEntityName:@"MovieDownloads"];
        self.moviesarray = [[managedobjectcontext executeFetchRequest:fetchrequest error:nil] mutableCopy];



        [insideMoviesManagedObject setValue:[[self.moviesarray objectAtIndex:self.SelectedTag]valueForKey:@"name"] forKey:@"mName"];
        [insideMoviesManagedObject setValue:[[self.moviesarray objectAtIndex:self.SelectedTag]valueForKey:@"path"] forKey:@"mPath"];
       [updateDevice setValue:[NSSet setWithObjects:insideMoviesManagedObject,nil] forKey:@"movieList"];

        // Save Managed Object Context
        NSError *error = nil;

        [updateDevice.managedObjectContext save:&error];
          [insideMoviesManagedObject.managedObjectContext save:&error];
        if (![updateDevice.managedObjectContext save:&error]) {
            NSLog(@"Unable to save managed object context.");
            NSLog(@"%@, %@", error, error.localizedDescription);
        }

0 个答案:

没有答案