Code Monkey home page Code Monkey logo

Comments (3)

heardrwt avatar heardrwt commented on July 26, 2024

Sounds like a good addition to have in the framework.

As for method names, perhaps:

-(NSArray*)unifiedPeople;
-(NSArray*)unifiedPeoplePreferringSource:(RHSource*)source;

or

-(NSArray*)peopleUnifiedUsingDefaultSource;
-(NSArray*)peopleUnifiedUsingSource:(RHSource*)source;

If you have some working code, feel free to post it or chuck it in a pull request.

from rhaddressbook.

0xdevalias avatar 0xdevalias commented on July 26, 2024

I like the second one better :) Was also thinking it would probably be beneficial to be able to unify any array of RHPerson's (for example, returned from peopleOrderedByUsersPreference, etc)

Been hacking around with it a little this afternoon, and this is what I came up with. The first part (based on stackoverflow) is quite clean, but I'm not the happiest with my next part for picking the record we want out of the unified set (pretty sure it's overkill and too laggy)

-(NSArray *)unifyPeople:(NSArray *)people
            usingSource:(RHSource *)source
{
    // Ref: http://stackoverflow.com/questions/11351454/dealing-with-duplicate-contacts-due-to-linked-cards-in-ios-address-book-api

    int defaultSourceID = (int)source.addressBook.defaultSource.recordID;
    int wantedSourceID = (int)source.recordID;

    NSPredicate *recordIdMatchesDefault = [NSPredicate predicateWithFormat:@"%K = %d",
                                           @"inSource.recordID",
                                           defaultSourceID];
    NSPredicate *recordIdMatchesSource = [NSPredicate predicateWithFormat:@"%K = %d",
                                          @"inSource.recordID",
                                          wantedSourceID];

    NSMutableOrderedSet *unifiedPeopleSet = [NSMutableOrderedSet orderedSet];
    NSMutableOrderedSet *unifiedPeopleForSourceSet = [NSMutableOrderedSet orderedSet];

    // Create set of unified contacts
    for (RHPerson *person in people)
    {
        NSMutableSet *contactSet = [NSMutableSet set];

        [contactSet addObject:person];
        [contactSet addObjectsFromArray:person.linkedPeople];

        [unifiedPeopleSet addObject:contactSet];
    }

    // Filter the set for our desired source (or default)
    for (NSSet *contactSet in unifiedPeopleSet)
    {
        RHPerson *uniquePerson = nil;
        NSSet *wantedSet = [contactSet filteredSetUsingPredicate:recordIdMatchesSource];
        NSSet *defaultSet = [contactSet filteredSetUsingPredicate:recordIdMatchesDefault];

        if (wantedSet.count >= 1)
        {
            uniquePerson = wantedSet.anyObject;
        }
        else if (defaultSet.count >= 1)
        {
            uniquePerson = defaultSet.anyObject;
        }
        else
        {
            uniquePerson = contactSet.anyObject;
        }

        [unifiedPeopleForSourceSet addObject:uniquePerson];
    }

    return [unifiedPeopleForSourceSet array];
}

from rhaddressbook.

nebiros avatar nebiros commented on July 26, 2024

Got the same issue, any resolution about this?.

from rhaddressbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.