如何使用sortDescriptor为一对多的关系的属性 [英] How to use sortDescriptor for an attribute on a to many relationship

查看:144
本文介绍了如何使用sortDescriptor为一对多的关系的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我有一个歌曲实体和一个播放列表实体。

  2. 播放清单可以有多首歌曲,而且歌曲可以连结到多个播放清单。

  3. 我有一个ListToSongs实体,它维护歌曲添加到播放列表的顺序。我不使用顺序关系在ios5(我知道它的存在,我需要更多的控制我的模型),我存储顺序在PlayListToSong实体中的sortOrder字段。因此,模式被建模为(关系名称写在下面的括号中):

Song(播放列表)< - >(song)ListToSong(playlist)< ---- >>(songs)List

Song (playlists) <<----> (song) ListToSong (playlist) <---->> (songs) List

我想使用获取请求获取给定播放列表的所有歌曲。

I want to fetch all Songs for a given PlayList using a fetch request.

ListToSong上的以下查询工作。这取得所有的PlayListToSong对象,我可以使用obj.song来获取Song对象。返回按sortOrder排序的数据。工作不错。 (下面的代码片段是为了简洁起见使用MagicalRecord编写的)。

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).

songFilter = [NSPredicate predicateWithFormat:@"playlist == %@", filterList];    
linkObjs = [FRListToSongs MR_findAllSortedBy:@"sortOrder" ascending:YES withPredicate:songFilter inContext:ctxt];

以下对Song实体的查询导致崩溃。如果我删除排序描述符,只是fetch,它的工作原理。所以问题是使用sortDescriptorplaylists.sortOrder,它试图根据ListToSongs实体中的属性进行排序。

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.

songFilter = [NSPredicate predicateWithFormat:@"ANY playlists.playlist == %@", filterList];    
songs = [FRSong MR_findAllSortedBy:@"playlists.sortOrder" ascending:YES withPredicate:songFilter inContext:ctxt];

在纯SQL中(下面给出),我试图在Song和ListToSong之间进行连接bysortOrder字段。

In plain SQL (given below), I am trying to do a join between Song and ListToSong and order by "sortOrder" field.

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

如何将上述SQL映射到coredata语法并给出正确的sortDescriptor?

How do I map the above SQL to coredata syntax and give the correct sortDescriptor?

请注意,唯一的问题是如何获取order by t1.ZSORTORDER的结尾,因为我不能正确地指定sortDescriptor。抓取请求无需排序描述符即可正常工作。

推荐答案

在我的问题。我在ListToSong实体上获取并使用obj.song获取相关的Song对象。我使用relationshipKeyPathsForPrefetching预取链接到ListToSong对象的歌曲对象。

I finally went ahead with "Approach 1" mentioned above in my question. I fetched on ListToSong entity and used obj.song to get the related Song object. I used relationshipKeyPathsForPrefetching to prefetch the song object linked to ListToSong object.

这篇关于如何使用sortDescriptor为一对多的关系的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆