Latest 2.2.1
Homepage https://github.com/levigroker/GRKArrayDiff
License Creative Commons Attribution 4.0 International License
Platforms ios 7.0, osx 10.9, tvos 9.0
Authors

Given two NSArrays, a previous and a current, GRKArrayDiff will report all deletions,
insertions, moves, and modifications. This is specifically targeted for array backed data
models which are used to support Table Views and Collection Views, but is generally
applicable as well.

Installing

If you’re using CocoPods it’s as simple as adding this to your
Podfile:

pod 'GRKArrayDiff'

otherwise, simply add the contents of the GRKArrayDiff subdirectory to your
project.

Documentation

To use, simply import GRKArrayDiff.h:

#import "GRKArrayDiff.h"

Then alloc and init a new instance, passing in the previous array, the new array, and two
blocks. The identityBlock is responsible for uniquely identifying the given object while
the modifiedBlock is responsible for reporting if the given currentObj object is to be
considered as modified.

GRKArrayDiff *diff = [[GRKArrayDiff alloc] initWithPreviousArray:previousArray currentArray:currentArray identityBlock:^NSString *(id obj) {
    return [obj identifier];
} modifiedBlock:^BOOL(id  _Nonnull previousObj, id  _Nonnull currentObj) {
    return [[currentObj identifier] isEqualToString:@"five"] ||
    [[currentObj identifier] isEqualToString:@"three"] ||
    [[currentObj identifier] isEqualToString:@"zero"] ||
    [[currentObj identifier] isEqualToString:@"one"];
}];

Once created, the instance’s four properties (deletions, insertions, moves, and
modifications) will be populated with NSSets of GRKArrayDiffInfo objects which
describe the changes to the elements between the previous and current arrays.

Typical iOS table view update usage would look something like this:

//Save the current data model for comparison
NSArray *oldDataModel = self.dataModel;

//Update our data model with the latest
self.dataModel = [self updateDataModel];

//Get differences in data model
GRKArrayDiff *diff = [[GRKArrayDiff alloc] initWithPreviousArray:oldDataModel currentArray:self.dataModel identityBlock:^NSString *(id obj) {
    NSString *identifier = nil;
    if ([obj conformsToProtocol:@protocol(MyType)])
    {
        id <MyType> myObj = (id <MyType>)obj;
        identifier = [myObj uuid];
    }
    return identifier;
} modifiedBlock:^BOOL(id  _Nonnull previousObj, id  _Nonnull currentObj) {
    BOOL modified = currentObj != nil && [modificationSet containsObject:currentObj];
    return modified;
}];

//Update the UI with the changes
;

Additional documentation is available in GRKArrayDiff.h and example usage
can be found in the GRKArrayDiffTest.m.

Disclaimer and Licence

About

A professional iOS engineer by day, my name is Levi Brown. Authoring a blog
grokin.gs, I am reachable via:

Twitter @levigroker
Email [email protected]

Your constructive comments and feedback are always welcome.

Latest podspec

{
    "name": "GRKArrayDiff",
    "version": "2.2.1",
    "summary": "Provides diffs between two NSArrays with details about deletions, insertions, moves, and modifications.",
    "description": "Given two NSArrays, an old and current, GRKArrayDiff will report all deletions, insertions,nmoves, and modifications. This is specifically targeted for array backed data models whichnare used to support Table Views and Collection Views, but is generally applicable as well.",
    "homepage": "https://github.com/levigroker/GRKArrayDiff",
    "license": "Creative Commons Attribution 4.0 International License",
    "authors": {
        "Levi Brown": "[email protected]"
    },
    "social_media_url": "https://twitter.com/levigroker",
    "source": {
        "git": "https://github.com/levigroker/GRKArrayDiff.git",
        "tag": "2.2.1"
    },
    "platforms": {
        "ios": "7.0",
        "osx": "10.9",
        "tvos": "9.0"
    },
    "source_files": "GRKArrayDiff/**/*.{h,m}"
}

Pin It on Pinterest

Share This