mirror of
https://github.com/Instagram/IGListKit
synced 2026-05-23 09:18:29 +00:00
Update IGListDiffable and Equality.md
Summary: Per #509 Closes https://github.com/Instagram/IGListKit/pull/513 Differential Revision: D4613737 Pulled By: jessesquires fbshipit-source-id: f77960819a047a0bd504dbf7ebf0558a569801cf
This commit is contained in:
parent
f63511a083
commit
85396f1236
1 changed files with 10 additions and 4 deletions
|
|
@ -1,10 +1,16 @@
|
|||
# IGListDiffable and Equality
|
||||
|
||||
This guide details how to write good `isEqual:` methods.
|
||||
This guide explains the `IGListDiffable` protocol and how to write good `-isEqual:` methods.
|
||||
|
||||
## Background
|
||||
|
||||
`IGListKit` requires that models implement the method `isEqualToDiffableObject:` which should perform the same type of check as `isEqual:`, but without impacting performance characteristics like in Objective-C containers such as `NSDictionary` and `NSSet`.
|
||||
The [`IGListDiffable` protocol](https://instagram.github.io/IGListKit/Protocols/IGListDiffable.html) requires clients to implement two methods, `-diffIdentifier` and `-isEqualToDiffableObject:`.
|
||||
|
||||
The method `-isEqualToDiffableObject:` should perform the same type of check as `-isEqual:`, but without impacting performance characteristics, like in Objective-C containers such as `NSDictionary` and `NSSet`.
|
||||
|
||||
Why are both of these methods required for diffing? The point of having the two methods has to do with **identity** and **equality**, where the diff identifier uniquely identifies data (common scenario is primary key in databases). Equality comes into play when comparing the values of two uniquely identical objects (driving reloading).
|
||||
|
||||
See also: [#509](https://github.com/Instagram/IGListKit/issues/509)
|
||||
|
||||
## `IGListDiffable` bare minimum
|
||||
|
||||
|
|
@ -22,7 +28,7 @@ The quickest way to get started with diffable models is use the _object itself_
|
|||
|
||||
## Writing better Equality methods
|
||||
|
||||
Even though `IGListKit` uses the method `isEqualToDiffableObject:`, the concepts of writing a good equality check apply in general. Here are the basics to writing good `-isEqual:` and `-hash` functions. Note this is all Objective-C but applies to Swift also.
|
||||
Even though `IGListKit` uses the method `-isEqualToDiffableObject:`, the concepts of writing a good equality check apply in general. Here are the basics to writing good `-isEqual:` and `-hash` functions. Note this is all Objective-C but applies to Swift also.
|
||||
|
||||
- If you override `-isEqual:` you **must** override `-hash`. Check out this [article by Mike Ash](https://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html) for details.
|
||||
- Always compare the pointer first. This saves a lot of wasteful `objc_msgSend(...)` calls and value comparisons if checking the same instance.
|
||||
|
|
@ -70,7 +76,7 @@ You would implement its equality methods like so:
|
|||
|
||||
## Using both `IGListDiffable` and `-isEqual:`
|
||||
|
||||
Making your objects work universally with Objective-C containers and `IGListKit` is easy once you've implemented `isEqual:` and `-hash`.
|
||||
Making your objects work universally with Objective-C containers and `IGListKit` is easy once you've implemented `-isEqual:` and `-hash`.
|
||||
|
||||
```objc
|
||||
@interface User <IGListDiffable>
|
||||
|
|
|
|||
Loading…
Reference in a new issue