Objective-C - Get Selected Object from NSArrayController - objective-c

I'm trying to create a many-to-many relationship in Core Data, and am having a good amount of trouble doing so - although I'm fairly new at Objective-C. I have two entities that I would like to tie together: Playlists and Songs. Each playlist will have a variety of songs, but each song can also be on several playlists, hence the many-to-many. After some looking around, I found that it may be easiest to do this by implementing a third entity which is essentially the "relationship" between the two. The Playlists would have a one-to-many relationship with the third entity, as would the Songs.. The reason for this would be so I could create / remove that third "relationship" entity when I want to add / remove a song from a playlist, and is better overall for organizing, at least in my opinion.
Last thing to note is that I have three basic tables in my window: A table that lists every playlist, a table that lists every song, and then a table that lists all the songs under whatever playlist is selected in the first table.
I'm currently stuck on how to create the "relationship" third entity based on the objects that are selected in both the playlists and songs arrays/tables. I understand how to create the entity, but don't know how I can call the array controller to give me the selected object. Any help or guidance would be greatly appreciated! Thanks in advance,
Pat

Related

Should I use a many-to-many relationship or obtain info through one-to-many relationships?

So I have a war-like game with three tables: User, Player, and Game.
The User table contains email, username, password hash, and other user-specific info.
The Game table contains info about each game and the state of each game world.
The Player table contains info about each player in a game: how many units they have, etc.
I have the following relationships:
A one-to-many relationship between User and Player, since each User can be in several different Games, and control several different Players simultaneously.
A one-to-many relationship between Game and Player, since each Game has multiple Players in it.
So in order to find the Games that a specific User is in, is it better practice to either:
Make a many-to-many relationship between User and Game, or
Write a function that takes the list of Players from a User, then find the Game IDs from the Player columns?
(I'm using SQLAlchemy with SQLite and Flask, but I don't think that's important to this question.)
The attached image may explain it better: should I have any database connections between User and Game?
I'd be glad to clarify anything, and thanks for any possible help! Sorry if this is a bad question for StackOverflow.
When a many-to-many relationship exists, say gameUsers, a common implementation is to create an intermediary table, containing the ids of each of the tables, in this case a list of user_id, game_id pairs representing which games each user is in.
But... you already have such a table, the player table. So my approach would be to think of the player table as that resolution table. The player table already contains which games each user is in, so that second many-to-many is not really required.

Showing relevant songs based on user data

I have this app where I have the genre of music that the user is liking and whenever a user listens to a song he rates it.I want to show them the most relevant songs that the users might like.How do I carry on the database schema?What all data should I take from a user and how to get the most relevant song for the particular user?
Clear Explanation is appreciated.
This is a very active field of research known as recommender systems. If you wish to deal with it seriously, database design is only a first step; you will need to think about a suitable algorithm etc. Your question was specifically about the database schema, so I recommend the following readings:
RecDB in Action: Recommendation Made Easy in
Relational Databases
Recommender system datastructures
Are you recommending the user songs based on his ratings only? In that case, you might need every user in your database to rate at least a few songs. If a user has not yet rated any songs (maybe he signed up for your application recently), you will not be able to recommend him any song. This problem, is called "The Cold Start Problem".
Coming to the question, you can group songs by either genre, artist, album, year or you can take any combination of these parameters. So, your Songs table will have these values along with name of the song, and probably a unique ID. Users table can be as small as UserID and name or you can include his age, location so that you can recommend him songs based on other user's likings with similar age and location.
There would also be a User-Song map table that contains the ratings that every user gives different songs (one-to-many mapping). This table can be used to find a user's favourite song(s)

Core Data - Multiple Entries for a Single Entity

So in my app I have an Entity called Cards and another called Transactions.
The Transaction entity have the attributes: Date, Location and Amount. So if a user spends his money in 10 locations, I need to have 10 entries in the Transactions for a single card.
I just started working with Core Data and it's getting messy.
I also use MagicalRecord to work with Core Data.
I was able to CRUD the cards entity, adding, updating etc... is all good.
The thing is, I need to add the transactions to a card and don't know how to start with this relationship. How to add transactions to the card and then fetch the card with all the transactions?
Any insight would be much appreciated.
If I understood you right, then you establish a relation between them.
Card -> Transaction is a To-Many relation. See the right hand pane in xcode for this option.
Add an inverse. Always add an inverse. So you have a not To-Many relation
Transaction -> Card
Right? There is only one card for each transaction?
The rest of the answer depends on how you access the data. I suggest to create a model class for each entity. You know how? Click on the entity then go to file/new/file, select core data then NSManagedObjectSubclass and it will be created for you. This class has methods for adding related items.
myTransaction.card = myCard;
respectively
[myCard addTransactionsObject:myTransaction];
Assuming of course that myCard and myTransaction are the classes and that your to-one relation is named card and the to-many relation is named transactions.
Just set a one-to-many relationship from Card to Transaction Entity, and then simply get all of your (previously added by [card addTransactionsObject:newTransaction] call) transactions using that relation: card.transactions.
Don't forget to add an inverse relation fromTransaction to Card!

Spotify API: Album name appears multiple times in a single query

I am receiving several multiple album names from the query listed below. I want to know if there is a way of receiving only one instance of the album name.
http://ws.spotify.com/lookup/1/?uri=spotify:artist:3TVXtAsR1Inumwj472S9r4&extras=album
Unfortunately, no.
Artists very often have multiple releases of the same album, for various reasons. Normally, it's slightly different versions of the album for different areas of the world - indeed, in the example you give the territories key has different values for each duplicate.
Depending on your use case, you need to deal with this correctly. If you're suggesting albums for a user to listen to, you need to make sure the album is available in the territory the user is in.
If you're just listing albums on a static page and not linking to them, putting the list of names though a set to thin out the duplicates would be sufficient.

How can I set up a newly created relationship in Core Data on iOS?

So I'm using Core Data in an existing iPhone app, and I've set up two entities: Person and Item. The root view of my app's navigation (currently) shows a list of people, and you drill down from there to items. In short, the hierarchy looks like this:
Person -> Item
I want to add a new entity above Person in the hierarchy, called List:
List -> Person -> Item
Additionally, I want the user's first List to be created for them on startup, and for any People the user's already added to be assigned to that list.
I'm familiar with Core Data's lightweight migration & versioning feature, so I think I know how to add the new entity and relationship, but I'm not sure how to:
Create a List record on app start if they've never had the Lists feature before
Set all existing People records to belong to that new list.
One quick and dirty way would be to add some code to my app delegate's applicationDidFinishLaunching method that performs the migration by (1) checking to see if there are any Lists, (2) if not, creating the default one, (3) fetching all existing People from my data store, (4) setting each Person's list attribute to the newly created default list, and finally (5) saving those changes.
My question is: is there any faster or easier way to do all of that?
That's pretty much what you'd want to do. Use an NSFetchRequest to see if any Listss exist. If not, create one. Then do another request to get all the Persons. Here, instead of assigning the list property of each Person, I'd create an NSSet containing all your Persons and assign that to the List's people property. You did create an inverse property, right?
This is actually a pretty lightweight operation, all tolled, so I wouldn't worry too much about performance. Unless you've got hundreds or thousands of Person objects, your user will probably won't even notice.