简体   繁体   中英

What is the most efficient way to setup relationships between thousands of objects in Core Data?

Let's say I have inserted 10.000 Book entities and 10.000 Author entities, and need to setup one to one relationships between them. Both entities have an attribute called bookID. I do this:

  1. Fetch all Books, sorted by bookID
  2. Fetch all Authors, sorted by bookID
  3. Enumerate the books array, and if currentBook.bookID == currentAuthor.bookID, then currentBook.author = currentAuthor. Repeat this for all books. At every 1000th book for which a relationship was just set, save the context (otherwise even more memory is consumed).

My problem is memory allocation going overboard. How do I limit that? I tried [fetchRequest setBatchSize:], setFetchLimit: in conjunction with setFetchOffset:, with no luck.

I don't think you are conceptualizing your data model correctly. An attribute "bookID" shouldn't really be in the Author Entity. Also it seems strange to have a one to one relationship in both ways. More likely you would want to have a one-to-many relationship from Author to Book (an author could write multiple books), and potentially even a one-to-many relationship in the opposite direction as well (a book could have multiple authors, although that is up to you).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM