Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use sortDescriptor for an attribute on a to many relationship
    primarykey
    data
    text
    <ol> <li>I have a Song Entity and a Playlist Entity. </li> <li>Playlist can have multiple Songs and Songs can be linked to multiple Playlist. </li> <li>I have a ListToSongs Entity which maintains the order in which Songs were added to the Playlist. I am not using "Ordered Relationship" in ios5 (I am aware of its existence and I need more control over my model) and I store the order by a "sortOrder" field in PlayListToSong entity. So the schema is modeled as (relationship names are written in parentheses below):</li> </ol> <p>Song (playlists) &lt;&lt;----> (song) ListToSong (playlist) &lt;---->> (songs) List</p> <p>I want to fetch all Songs for a given PlayList using a fetch request.</p> <p>The following query on ListToSong works. This fetches all the PlayListToSong objs and I can use obj.song to get the Song object. Gives back the data sorted on sortOrder as well. Works good. (The code snippets below are written using MagicalRecord for reasons of brevity).</p> <pre><code>songFilter = [NSPredicate predicateWithFormat:@"playlist == %@", filterList]; linkObjs = [FRListToSongs MR_findAllSortedBy:@"sortOrder" ascending:YES withPredicate:songFilter inContext:ctxt]; </code></pre> <p>The following query on Song entity results in a crash. If I remove the sort descriptor and just fetch, it works. So the issue is with the sortDescriptor "playlists.sortOrder" which is trying to sort based on the attribute in the ListToSongs entity.</p> <pre><code>songFilter = [NSPredicate predicateWithFormat:@"ANY playlists.playlist == %@", filterList]; songs = [FRSong MR_findAllSortedBy:@"playlists.sortOrder" ascending:YES withPredicate:songFilter inContext:ctxt]; </code></pre> <p>In plain SQL (given below), I am trying to do a join between Song and ListToSong and order by "sortOrder" field. </p> <pre><code>SELECT DISTINCT 0, t0.Z_PK, t0.Z_OPT, t0.ZNAME, t0.ZSEQ FROM ZSONG t0 JOIN ZFRLISTTOSONGS t1 ON t0.Z_PK = t1.ZSONG WHERE t1.ZPLAYLIST = ? order by t1.ZSORTORDER </code></pre> <p>How do I map the above SQL to coredata syntax and give the correct sortDescriptor? </p> <p><em>Please note the only issue is to how to get "order by t1.ZSORTORDER" at the end since I am not able to specify the sortDescriptor correctly. The fetch request works fine without the sort descriptor.</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload