开发者

CoreData Adding a new Relationship to an Entity row

So here's the problem:

My project got 2 Entities. The first one is Organisation the second one is Brands. They are connected with an 1 to Many relationship. "A Organisation can have multiple Brands" Everything is working for that. The Tableview with the Organisation and the Brandtableview and a DetailView.

What I want is to add a Favourites Feature to my app where i connect a Favouritelist with Brands i like. Therefore i added the Entity "Favourits" to the xcdatamodel. Every FavouriteList has a Name and it is connected to "Brands" with a many to many relationship."A Favouritelist can have multiple Brands and EBrands" could be in multiple Favouritelists.

to create a new List have the following code

currentFav = (favoriten*)[NSEntityDescription insertNewObjectForEntityForName:@"favoriten" inManagedObjectContext:managedObjectContext];
[currentFav setName:@"MeineFavoriten"]; [self saveAction];

In the DetailView of the Brands I have a addToFav Action and i want to update the Brand in Brands with the relationship to the Favouritelist.

Example: "Link the Brand with n开发者_如何学编程ame "XY" to MeineFavouriten of favoriten"

How can i do that?


To-many relationships are sets so you can use a simple setValue call. Instead you first have to get a mutable set, add an object to the set and then set the mutated set as the value.

NSMutableSet *favoriteBrands=[aFavoriteList mutableSetValueForKey:@"brands"];
[favoriteBrands addObject:aNewBrandObj];
[aFavoriteList setValue:favoriteBrands forKey:@"brands"];

If you create a class for FavoriteList, you get methods that let you add objects easily to to-many relationships without having to write three lines of code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜