Coder Social home page Coder Social logo

apparentsoft / grkarraydiff Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mobaxe/grkarraydiff

0.0 1.0 0.0 39 KB

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: Other

Ruby 1.17% Objective-C 98.83%

grkarraydiff's Introduction

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 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
[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
Email [email protected]

Your constructive comments and feedback are always welcome.

grkarraydiff's People

Contributors

levigroker avatar vlcone avatar

Watchers

 avatar

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.