Latest 0.1.3
Homepage https://github.com/lintmachine/LMArrayChangeSets
License MIT
Platforms ios 6.0, requires ARC
Authors

CI Status
Version
License
Platform

Usage

This is a set of simple categories that help with a common pattern I use for updating array-backed UITableViews and UICollectionViews.

It extends NSArray to provide simple diff functionality. You provide the initial array, the updated array, and an identity comparison block, and the method returns an NSDictionary of NSIndexSets with the inserted, deleted, moved and unmoved indexes.

Categories are provided for UITableView and UICollectionView that take the IndexSet dictionary and perform the updates as a batch with row/cell animation. Items that have not moved from their initial position may optionally be reloaded.

Objective-C Examples

UICollectionView

- (void)updateWithItems:(NSArray *)items {

    NSDictionary* indexChangeSets = [NSArray indexChangeSetsForUpdatedList:items
                                                              previousList:self.items
                                                        identityComparator:^NSComparisonResult(NSDictionary* a, NSDictionary* b) {
                                                            // This block should determine if the items in the list have the same identity or not
                                                            return [a[@"id"] compare:b[@"id"]];
                                                        }];

    [self.collectionView performBatchUpdates:^{
        self.items = items;
        [self.collectionView updateSection:0 withChangeSet:indexChangeSets reloadingUnmoved:YES];
    } completion:nil];
}

UITableView

- (void)updateWithItems:(NSArray *)items {

    NSDictionary* indexChangeSets = [NSArray indexChangeSetsForUpdatedList:items
                                                              previousList:self.items
                                                        identityComparator:^NSComparisonResult(NSDictionary* a, NSDictionary* b) {
                                                            // This block should determine if the items in the list have the same identity or not
                                                            return [a[@"id"] compare:b[@"id"]];
                                                        }];
    self.items = items;
    [self.tableView beginUpdates];
    [self.tableView updateSection:0 withChangeSet:indexChangeSets];
    [self.tableView endUpdates];
}

Swift Examples

Note: To use this library in Swift, you must import the library header file into your project’s Bridging Header file.

#import "LMArrayChangeSets.h"

More details about mixing Objective-C and Swift code are available here.

UICollectionView

func updateWithItems(items:NSArray) {

    let indexChanges = NSArray.indexChangeSetsForUpdatedList(items, previousList: self.items) {
            (anyA:AnyObject!, anyB:AnyObject!) -> NSComparisonResult in
            let a = anyA as MyObject
            let b = anyB as MyObject
            // This should determine if the items have the same identity or not
            return a.id.compare(b.id)
    }

    self.collectionView!.performBatchUpdates({
        () -> Void in
        self.items = items
        self.collectionView!.updateSection(0, withChangeSet:indexChanges)
        },
        completion:nil
    )
}

UITableView

func updateWithItems(items:NSArray) {

    let indexChanges = NSArray.indexChangeSetsForUpdatedList(items, previousList: self.items) {
            (anyA:AnyObject!, anyB:AnyObject!) -> NSComparisonResult in
            let a = anyA as MyObject
            let b = anyB as MyObject
            // This should determine if the items have the same identity or not
            return a.id.compare(b.id)
    }

    self.items = items
    self.tableView!.beginUpdates()
    self.tableView!.updateSection(0, withChangeSet:indexChanges, reloadingUnmoved:true)
    self.tableView!.endUpdates()
}

Performance

Please Note: The algorithm used to determine the change sets is not particularly efficient. O(N^2)

The library was created with developer efficiency in mind, rather than raw performance. As such, it may not perform well with large data sets.

In my experience, the performance has been perfectly acceptable for data sets with counts in the hundreds. If you plan to use this with larger data sets, you’ll want to do some performance testing first and optimize your comparison routine.

I recommend generating the change sets on a background thread.

Requirements

iOS 6.0+

Installation

LMArrayChangeSets is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "LMArrayChangeSets"

To run the example project, clone the repo, and run pod install from the Example directory first.

Author

cdann, [email protected]

License

LMArrayChangeSets is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "LMArrayChangeSets",
    "version": "0.1.3",
    "summary": "Categories to help in updating array-backed UITableViews and UICollectionViews with row/cell animations.",
    "description": "This is a set of simple categories that help with a common pattern I use for updating array-backed UITableViews and UICollectionViews.nnIt extends NSArray to provide a simple diff functionality. You provide the initial array, the updated array, and an identity comparison block, and the method returns an NSDictionary of NSIndexSets with the inserted, deleted and moved indexes.nnCategories are provided for UITableView and UICollectionView that can take the IndexSet dictionary and perform the updates as a batch with row/cell animation.nn",
    "homepage": "https://github.com/lintmachine/LMArrayChangeSets",
    "license": "MIT",
    "authors": {
        "cdann": "[email protected]"
    },
    "source": {
        "git": "https://github.com/lintmachine/LMArrayChangeSets.git",
        "tag": "0.1.3"
    },
    "social_media_url": "https://twitter.com/lintmachine",
    "platforms": {
        "ios": "6.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*",
    "resource_bundles": {
        "LMArrayChangeSets": [
            "Pod/Assets/*.png"
        ]
    }
}

Pin It on Pinterest

Share This