Reputation: 105
I have 2 entities in CoreData. Deck
and Flashcard
. Deck
has an ordered to-many relationship with Flashcard
. This means that Deck
contains property flashcards: NSOrderedSet
containing the Flashcard
s.
In a DeckView
I try to show all Flashcard
s from a certain Deck
in a user-specified order. For that I use FetchRequest
.
static func fetchInDeck(deck: Deck) -> NSFetchRequest<Flashcard> {
let request = Flashcard.fetchRequest()
request.predicate = NSPredicate(format: "deck == %@", deck)
request.sortDescriptors = []
return request
}
The flashcards maintain order until I start adding items, new items get added in seemingly random places, but after restarting the app they go where they should. (e.g. after adding 10 items the order was [3 5 7 9 10 8 6 4 2 1], and after restart [10 9 8 7 6 5 4 3 2 1]) For adding I used a method generated automatically by Xcode.
let newFlashcard = Flashcard(context: context)
insertIntoFlashcards(newFlashcard, at: 0)
Is there a sort descriptor that will cause those items to stay in order? If not is there another way to maintain the order?
Upvotes: 1
Views: 102