Optimizing this Core Data request

前端 未结 1 1270
萌比男神i
萌比男神i 2021-01-15 18:14

I have an entity in Core Data named MusicInterest. I have to add 5000 or so of these at a time and my current process is to query to see if the MusicInterest exists already,

1条回答
  •  不要未来只要你来
    2021-01-15 18:55

    Implementing Find-or-Create Efficiently in the "Core Data Programming Guide" describes a pattern that might be useful here. The basic idea is:

    • Sort your list of items that you want to insert/update by some unique id that is also stored in the database.
    • Perform a single fetch request that fetches all objects from the database that have an id from your list, sorted by the same id.
    • Now traverse your list and the array of fetched items in parallel, to find which items have to be inserted and which items already exist and can be updated.

    0 讨论(0)
提交回复
热议问题