Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Sorting by distance #106

Closed
dneykov opened this issue Jun 20, 2017 · 6 comments
Closed

Sorting by distance #106

dneykov opened this issue Jun 20, 2017 · 6 comments

Comments

@dneykov
Copy link

dneykov commented Jun 20, 2017

Hi,
I'm loading a list of locations in tableView. I have a model with fields "latitude" and "longitude" and I want to sort this list by distance using my current location. How can I sort dataSource?
Thanks.

@3lvis
Copy link
Owner

3lvis commented Jun 20, 2017

Hi @dneykov,

Doesn't seem that transient properties are supported for Core Data sort descriptors.

https://stackoverflow.com/a/13758006/717416

@dneykov
Copy link
Author

dneykov commented Jun 20, 2017

I was wondering if its possible some how to sort all objects using sort() function on dataSource.all()

@3lvis
Copy link
Owner

3lvis commented Jun 20, 2017

you can use the sortDescriptor for distance but it means you will need a distance attribute that gets updated every time your current location changes.

@dneykov
Copy link
Author

dneykov commented Jun 22, 2017

Can I sort it in memory after it's fetched from CoreData? I'm trying something like that but is not working:

self.dataSource.all().sort({ (objectOne, objectTwo) in
            guard let _objectOne = objectOne as? Object else { return false }
            guard let _objectTwo = objectTwo as? Object else { return false }
            
            return _objectOne.distance(to: lastKnownLocation) < _objectTwo.distance(to: lastKnownLocation)
        })

@3lvis
Copy link
Owner

3lvis commented Jun 22, 2017

@dneykov I don't think NSFetchedResultsController works with in-memory data, you need to store it in disk.

@dneykov
Copy link
Author

dneykov commented Jun 22, 2017

@3lvis I took your advise to update distance attribute. Every time before segue to that list view i'm updating distance attribute with last know location and it works perfectly.
Not sure if it's the right way but is working :)

Thanks

@dneykov dneykov closed this as completed Jul 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants