Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equatable intersects with other comparing parts in project #4

Open
mojidabckuu opened this issue Jan 31, 2018 · 2 comments
Open

Equatable intersects with other comparing parts in project #4

mojidabckuu opened this issue Jan 31, 2018 · 2 comments

Comments

@mojidabckuu
Copy link

Diffable uses == operator to determine if two objects are same or not. If you are using ListDiff when some model stores visual information and in the same time is a part of another system which is basing on == operator it breaks the logic.
For example you have a Post and each post has a badge of views.

class Post: Diffable {
  var id: Int
  var badge: Int

 func ==(lhs: Post, rhs: Post) -> Bool { return lhs.id == rhs.id && lhs.badge == rhs.badge }
 var diffIdentifier: AnyHashable { return self.id }
}

The PK is id attribute. Badge is just a number to render and doesn't represent object itself. Let's assume you want to refresh your views since badge is not the same. Then badge becomes a part of ==. So if you are trying to lookup though the array of posts having id only then it requires write blocks like where: { $0.id == some_id } which is not correct and other libs based on == couldn't success to find an object, etc.

Expected behaviour is to have an independent operator/method to differentiate two objects for updates.

@lxcid
Copy link
Owner

lxcid commented Feb 8, 2018

I think Diffable is mostly only providing an identity mechanism. The equality is handled by Equatable.

public static func diffing<T: Diffable & Equatable>(oldArray:Array<T>, newArray:Array<T>) -> Result {

The part where equatable come into play is in these lines where we detect if the the 2 identical element are not equal.

if oldIndex < oldArray.count {
let n = newArray[i]
let o = oldArray[oldIndex]
if n != o {
entry.updated = true
}
}

I think semantically wise, this is correct. If something is not equal, ListDiff will let you know about it.

In your case, you might want a ViewModel representation so that you does not need to populate your model with view related attributes.

struct PostViewModel: Diffablel {
  post: Post
  badge: Int

  var diffIdentifier: Int {
    return self.post.id;
  }
}

Pardon me if there's syntax error, I'm kinda rusty with Swift nowadays.

@mojidabckuu
Copy link
Author

That's correct. I propose to add an extra operator to compare 2 elements in terms of diffing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants