1

I have an entity Contact which has duplicate contact object. Eg :

|---------------------|------------------|---------------|
|     Name            |    Index Value   |   Number      |
|---------------------|------------------|---------------|
|  Daniel Higgins     |         4        |    12345      |
|---------------------|------------------|---------------|
|  Daniel Higgins     |         4        |    123456789  |

Now here i am fetching the contact using NSFetchedResultsController and using it for UITableviewController.

Here i want to show only one entry for name Daniel Higgins. How can i filter out the unique objects from NSFetchedResultsController based on idexvalue.

Kapil_A
  • 125
  • 2
  • 15

2 Answers2

1

You can create your request like this (where ContactResult is your generated class from core data):

    let request = NSFetchRequest<ContactResult>(entityName: "contacts")
    request.propertiesToFetch = ["name"]    // an array of properties between which should be distinguished
    request.returnsDistinctResults = true   // dont do duplicates

This way it is not possible to get other properties like number as well, otherwise they would be distinct again and Daniel Higgins will pop up twice again. If thats actually what you want (get name and at least one number (don't care which)), then you could also use group by:

    let request = NSFetchRequest<ContactResult>(entityName: "contacts")
    request.propertiesToGroupBy = ["name"]    // an array of properties between which should be grouped if there are duplicates

Warning: Which number gets chosen if there are multiple, may not be consistent!

However, then you could directly fetch the results as suggested by Mehul Parmar:

    context.fetch(request)

or instead use a Controller as you suggested and therefore make use of its delegate and be notified of upcoming changes:

    let controller = NSFetchedResultsController(
        fetchRequest: request,
        managedObjectContext: context,
        sectionNameKeyPath: nil,    // just for demonstration: nil = dont split into section
        cacheName: nil              // and nil = dont cache
    )
FlixMa
  • 944
  • 1
  • 7
  • 20
  • @KixxOne I'm trying to implement your solution but I still get duplicates from my `FetchReultController`. Could you please have a look at my question? here's the link: https://stackoverflow.com/questions/56168119/removing-duplicate-objects-from-fetch-based-on-object-parameter-swift Many thanks – Vincenzo May 16 '19 at 16:00
  • adding on this answer you have to declare your controller as `NSFetchedResultsController`, check my question in above comment for complete guide. – Vincenzo May 19 '19 at 09:00
0

When you create a fetchResultsController request object, apply a predicate to it to filter out entries based on the name you want.

Then, when you fetch the objects using context.fetch(request), if there are multiple entries, you can simply use the first one.

Mehul Parmar
  • 3,599
  • 3
  • 26
  • 42
  • The problem here is, that he needs to know the name of the duplicated contact. I don't think we can take that for granted. Instead you could try a `.reduce()` on the `fetchedValues` variable, always checking if the last result already contains the current one. – FlixMa Mar 01 '18 at 13:16
  • IF that is the case, then yes, fetch all entries first, then add custom filter logic to remove duplicate entries. Else, use propertiesToFetch followed by returnsDistinctResults for index property. – Mehul Parmar Mar 01 '18 at 13:24