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

Method for both removing and adding annotations #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

virl
Copy link

@virl virl commented Dec 29, 2014

Method for both removing and adding annotations in one go. Useful when integrating with CoreData.

@choefele
Copy link
Owner

choefele commented Jan 7, 2015

Sorry for the late response and thanks for your pull request.

I was thinking to support this use case in a similar way to UITableView by surrounding add/remove calls with beginUpdates and endUpdates.

I think this would make for a cleaner API. What do you think?

@virl
Copy link
Author

virl commented Jan 7, 2015

Yes, it will make more universal API, but for now I think it is overengineering. Let's accept my pull request and if someone will need begin/end updates then convert api to it.
Because when such updates are required, they are produced and accumulated on background thread in main application (as in my case) and begin/end updates will not simplify anything really.

@virl
Copy link
Author

virl commented Feb 5, 2015

Hi. Will you accept this PR?

@mikrobi
Copy link

mikrobi commented Mar 28, 2015

What's the status here?

@choefele
Copy link
Owner

I'm not really sure if the proposed API is a good idea. I was thinking about a beginUpdates/endUpdates style API.

@mikrobi In what context would this feature be useful to you?

@mikrobi
Copy link

mikrobi commented Mar 28, 2015

Actually I agree with you. After taking a deeper look I'm just working with the currently available methods ;)

@ryancrunchi
Copy link

Hello,
thanks for your work. I need this feature because, as @virl, I'm using a map view with CoreData and NSFetchedResultsController as a datasource. I then use NSFetchedResultsControllerDelegate methods to update my map. So, for each updated objects in my fetchedResultsController I need to remove its annotation and re-add it in order to get it state refreshed. When I call both removeAnnotations:withCompletionHandler: and addAnnotations:withCompletionHandler: just after, I have duplicated annotations on map.

@markkrenek
Copy link

A beginUpdates/endUpdates style API would be great. But here's how I solved this for my NSFetchedResultsController case using the current code base:

Instead of performing add/removeAnnotations for each update, just collection those annotations in a couple of arrays and them batch process them in controllerDidChangeContent.

private var homesAddedByFRC: [Home]?
private var homesRemovedByFRC: [Home]?

func fetchedResultsControllerWillChangeContent(controller: FetchedResultsController<Home>)
{
    // Handle adds and removes as a large batch.
    homesAddedByFRC = []
    homesRemovedByFRC = []
}

func fetchedResultsController(controller: FetchedResultsController<Home>, didChangeObject change: FetchedResultsObjectChange<Home>)
{
    switch change
    {
        case let .Insert(home, _):
            homesAddedByFRC.append(home)

        case let .Delete(home, _):
            homesRemovedByFRC.append(home)

        case let .Move(home, _, _):
            homesRemovedByFRC.append(home)
            homesAddedByFRC.append(home)

        case let .Update(home, _):
            homesRemovedByFRC.append(home)
            homesAddedByFRC.append(home)
    }
}

fetchedResultsControllerDidChangeContent(controller: FetchedResultsController<Home>)
{
    // Handle removed homes first because in some instances, we removed and then added the same home in order to get it updated.
    self.removeAnnotationsForHomes(homesRemovedByFRC!)
    self.addAnnotationsForHomes(homesAddedByFRC!)

    homesAddedByFRC = nil
    homesRemovedByFRC = nil
}

This code snippet is not a direct copy/paste from my code, so it may not be 100% syntactically correct.

@choefele
Copy link
Owner

@markkrenek Thanks for your example

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

Successfully merging this pull request may close these issues.

5 participants