0

I found many tutorials & articles over internet for core data but I am making sample app that is realtime & more useful to beginner. You can find the sample code here.

The core data structure core data structure is like this.

I make entries to artists, albums & songs. But the problem is later editing done to already added Artists is not reflecting to albums.

What am i missing to do so?

To reproduce this issue I request to download the project & run it.

  • Make some entries to Artists, Albums, & Songs.
  • Then later on goto to Artist tab & edit some of the the Artist entries.
  • Now goto Album tab. You will see the artists name same as old ones for associated Album entries.

This is the problem. Ideally I should see new entries there.

I want some genuine way to achieve this

hp iOS Coder
  • 3,230
  • 2
  • 23
  • 39
  • 1
    Do you use multithreading? P.S. don't want to download the zip, unarchive, search the code, it's better for you to provide some technical details or put the code somewhere else, so we could find it in one click without wasting or time. – MANIAK_dobrii Mar 15 '13 at 07:29
  • No multi-threading. My question is what I am missing in the code? I have not written something that will automatically reflect the changes. So sorry for your inconvenience. I dont see any related code to post here. – hp iOS Coder Mar 15 '13 at 07:37

1 Answers1

1

Not sure about your instructions on reproducing the issue as the Album tab doesn't show Artist's names, until you click the Genre button, and then it shows the artists names correctly. The spot where I saw an error was in going to the Song tab.

From a brief look at what's happening I believe this problem is because you are displaying the artist name by the NSString saved into your song called songArtist as opposed to getting the artist name from a referenced artist entity.

CoreData will not update song.artistName NSString just because you changed artist.artistName. It doesn't know that those 2 strings are related. You will need to do that update yourself.

Alternatively, you need to refresh the information from the Song entity to set the new variable of Song's artistName.

Hope that makes sense.

/* UPDATE */

Your Song references an Artist, but it is a to-many relationship. I always assumed a Song would only have one artist, so untick the to-many relationship in your CoreData relationship. In which case you should then display the artist name by way of song.artist.artistName. Then when the artist entity is updated the song will be referencing the artist which in turn has the correct info.

DonnaLea
  • 8,643
  • 4
  • 32
  • 32
  • +1 for the illustration. I got ur point that I am displaying artists names on **Album** tab from `NSString` stored into `albumGenre` attribute from *Album* entity, as opposed to getting the artist name from referenced *Artist* entity. Similarly for **Song** tab album names are from `NSString` stored in `songAlbum` attr of *Song* entity. So my question is what am I supposed to do? How can I show the artists name on **Album** tab list by referencing to *Artist* entity. – hp iOS Coder Mar 15 '13 at 11:08
  • I got ur logic behind updated answer. So to try it, I have same relationship structure between *Song* & *Album*. One song belongs to only one album. I tried writing `Album *referencedAlbum = song.album;` and `NSString *refAlbumName = referencedAlbum.albumName;` But failed. xcode is not showing `referencedAlbum.albumName` in intelligence. – hp iOS Coder Mar 16 '13 at 07:29
  • @hpiOSCoder then you probably haven't imported Album.h (#import "Album.h") – DonnaLea Mar 16 '13 at 22:12
  • Accepted the answer. Fantastic.. Simply love you... I was missing the `#import "Album.h"`. Also in `SongDetailViewController.m` file I was missing the assignment `[editingSong setAlbum:selectedAlbum];`. Anyways.. Excellent help from you DonnaLea. I wish I could upvote answer one more time. :) – hp iOS Coder Mar 18 '13 at 14:14
  • Hey DonnaLea I want one more favor from you. Case in which there is to-many relationship between both entities, how to deal with this.?? Coz in such case CoreData generated `NSManagedObject` class *entity.h* has the `NSSset` object for associated relational entity. – hp iOS Coder Mar 18 '13 at 14:32
  • 1
    @hpiOSCoder if you have an NSSet of multiple entities, then you would loop through and get the value you want from each of the entities. E.g. Loop through an NSSet of Artists, you would make an NSMutableString and append to that string each Author name so you can display all Artists names in the one place. It really depends on why you have a to-many relationship and how you want to treat them, that is one example. – DonnaLea Mar 20 '13 at 07:14
  • Hi DonnaLea. I need your help. Here is my code https://www.dropbox.com/s/qfeh19zcs0sz91z/CDBestTutorial.zip. After adding entries to Artists & then using them to Album is causing problem. If I use the same artist object for more than 2 albums then it is losing reference to previous album & get assigned to newly assigned album. In any case 1 Artist belongs to only one album, not more than that. What wrong Am I doing? (PS:Song is not working) – hp iOS Coder Apr 08 '13 at 11:36