CoreData:添加两级 fetchRequest
我有一个存储电影和演员的核心数据。 我通过json获取电影列表。每部电影都有一个演员(演员,角色),
所以我有电影,演员,ActorRole 实体。 当我收到电影列表时(每部电影都有一个唯一的 movieid): - 获取我的数据库中当前的所有电影 - 我创建一个现有MoviesID的NSSet - 我创建了一个 newMoviesID 的 NSSet - 我做了一个减集来获得所有真正的新电影 - 我循环遍历这些 newItems 将它们添加到 CoreData
这非常快,因为我只执行一个获取请求。 现在,当我想将转换添加到数据库时,它变得非常慢 添加新电影时我现在要做的事情: - 获取演员ID - 为该 Actor ID 创建 FetchReqyest - 执行FetchRequest - 如果找不到,我添加一个新演员,否则在 CoreData 中获取该演员 - 我创建了 ActorRole,它创建了电影和演员之间的关系,
我认为这非常慢,因为我为每部电影执行一次 Fetch 所以我正在寻找更好的方法来实现它。
有什么想法吗?
谢谢
I have a Core Data that stores Movies and Actors.
I get the list of movies through json. Each Movie has a cast(Actor, role)
So I have Movie, Actor, ActorRole entity.
When i receive the list of movies(each movie has a unique movieid):
- Fetch all movies currently in my database
- i create a NSSet of existingMoviesID
- i create a NSSet of newMoviesID
- i do a minus set to get all really new movies
- i loop through those newItems to add them to CoreData
This is pretty fast as i do only one fetch request.
Now it gets very slow when i want to add the cast to the database
What i do now when adding a new movie:
- get Actor ID
- create FetchReqyest for that Actor ID
- execute the FetchRequest
- if not found i add a new actor, else get the one in the CoreData
- i create the ActorRole which create the relationship between the Movie and the Actor
I think this is amazingly slow because i do one Fetch for each movie
So i am looking for better way to accomplish it.
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
IN
运算符以数组中的 actor_ID 值获取所有 Actor 对象,从而一次性获取所有现有的Actor
对象。就像这样:这会加快你的
Actor
获取速度,这似乎是你的瓶颈。You could fetch all the existing
Actor
objects in one go by using theIN
operator to fetch all the Actor objects with actor_ID values in an array. Like so:That would speed up your
Actor
fetch which appears to be your bottleneck.