Skip to content

Given two NSArrays, an old and 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.

License

Notifications You must be signed in to change notification settings

mobaxe/GRKArrayDiff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GRKArrayDiff

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 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 obj) {
	return [[obj identifier] isEqualToString:@"five"] ||
	[[obj identifier] isEqualToString:@"three"] ||
	[[obj identifier] isEqualToString:@"zero"] ||
	[[obj 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 obj) {
    BOOL modified = obj != nil && [modificationSet containsObject:obj];
    return modified;
}];

//Update the UI with the changes
[diff updateTableView:self.tableView section:0 animation:animated ? UITableViewRowAnimationAutomatic : UITableViewRowAnimationNone completion:nil];

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
App.net @levigroker
Email levigroker@gmail.com

Your constructive comments and feedback are always welcome.

About

Given two NSArrays, an old and 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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published