-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Question about using IGListKit with Photos framework #454
Comments
@andreamazz I think the last line of the function, public func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
guard let object = object as? LibraryItem else { return false }
return id != object.id
} should be |
Hi @zhubofei extension LibraryViewController: PHPhotoLibraryChangeObserver {
public func photoLibraryDidChange(_ changeInstance: PHChange) {
assets = PHAsset.fetchAssets(with: sortDescriptor)
data = [LibraryItem(id: Int(Date().timeIntervalSince1970), with: assets)]
DispatchQueue.main.async {
self.adapter.performUpdates(animated: true, completion: nil)
}
}
} |
Unless I'm mistaken IGLK will refresh on a section by section basis by default, your implementation is designed around one section which has many cells when in reality the data structure should be changed to be 1 section per asset (in your case). The reason it's always refreshing is because your library item ID is the time, which is always changing. So from IGLK point of view, it's a completely different item and will do a full refresh. |
@Sherlouk thanks for your answer. This leads to the question that I had in mind when I decided to proceed with the single section: since the datasource needs to provide a Also, with an item per section would it be possible to achieve a grid layout? (like the stock photo library). Thank you for you time. |
Yes that would mean some sort of iterative creation of items from the results you get, without a much bigger look into your implementation/actually how the photo library stuff works I couldn't give a very good answer on how best to do it It's possible to do a grid layout if an item per section yes, in fact @zhubofei created a custom layout just for this (which is packaged with IGListKit). See https://github.com/Instagram/IGListKit/blob/master/Examples/Examples-iOS/IGListKitExamples/ViewControllers/StoryboardViewController.swift. (Uses the layout) |
Great, thank you @Sherlouk |
|
@zhubofei |
😆 |
@andreamazz I SEE 😂. Also worth noting, there is a much better grid layout to be released soon. Check out #450. |
@andreamazz awesome question! @Sherlouk and @zhubofei summed everything up perfectly.
I'd recommend just extending extension PHAsset: IGListDiffable {
func diffIdentifier -> NSObjectProtocol {
return localIdentifier
}
func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
return isEqual(object: object) // PHObject implements this for you
}
}
Don't worry about this part. As long as your equality method is performant, the framework will be fast. In Instagram we diff hundreds/thousands of items all the time, on the main queue, and don't skip miss a frame 😄 |
Would be much appreciated if we can use Hashable instead of IGListDiffable in swift, as we have to include IGListKit into our core framework in my project, which is really unnecessary. |
Hey @MichaelLuoSyd! We’re brainstorming some ways to make IGListKit and Swift better. A new issue to discuss this as an option would be great! You should be able to add all conformance via extensions, hopefully solving any import issues. Obviously IGListKit has to be imported somewhere in order to use it, though. Sent with GitHawk |
@rnystrom Thanks very much for your quick response. The data models in the core frameworks are all RealmSwiftObjects. In my project, I tried to add extensions to the models as below: import Foundation extension CompanyProfile: ListDiffable { Swift compiler failed with error message "Cannot define category for undefined class 'CompanyProfile'", but the 'CompanyProfile' class does exist in the core framework Any idea what went wrong? Thanks |
That’s really bizarre, sounds like some sort of project setup issue? I’d imagine that extending a class in another framework should work just fine 🤔 Sent with GitHawk |
that's what i thought. The temporary solution is to import IGListKit to the core framework to make the compiler happy |
README
and documentationI'm trying to use IGListKit to present a library picker that fetches the user's pictures from the camera roll, so my datasource is basically a
PHFetchResult<PHAsset>
instance, that fetches the single images asynchronously:For the id I use a timestamp of the latest fetch.
The section controller is something along these lines:
This works fine, but when an update to the camera roll is triggered, the whole collection view is refreshed (and you can see the cell "flashing" a couple of times the re-render) since the diffing considers the whole library changed.
I was wondering if there is a better approach to this problem. Thanks.
The text was updated successfully, but these errors were encountered: