magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

mr_fetchAllSorted fetches not only the parent entity but also the child entity #1322

Closed GordonNY closed 5 years ago

GordonNY commented 6 years ago

For example, A is the parent entity of B. A.mr_fetchAllSorted() returns the objects including both A and B..

Coeur commented 6 years ago

Good point. And it appears that you can't filter out subclasses at predicate level: https://stackoverflow.com/questions/16636338/nspredicate-that-filters-out-subclass-results

I see three workarounds (for MagicalRecord 2.x):

perform fetch twice (least efficient)

    let controller = A.mr_fetchAllSorted(by: "id", ascending: true, with: nil, groupBy: nil, delegate: nil)
    controller.fetchRequest.includesSubentities = false
    A.mr_performFetch(controller)
    let results = controller.fetchedObjects

filter the results (average solution)

    let controller = A.mr_fetchAllSorted(by: "id", ascending: true, with: nil, groupBy: nil, delegate: nil)
    let results = controller.fetchedObjects?.filter { type(of: $0) == A.self }

don't use mr_fetchAllSorted (most efficient)

    let request = A.mr_requestAllSorted(by: "id", ascending: true, with: nil)
    let controller = A.mr_fetchController(request, delegate: nil, useFileCache: false, groupedBy: nil, in: NSManagedObjectContext.mr_contextForCurrentThread())
    controller.fetchRequest.includesSubentities = false
    A.mr_performFetch(controller)
    let results = controller.fetchedObjects