NSSearchField and ArrayController removeObjects issue

I created a Artist/Genre table based on the File table example given in Chapter 5- Extra Columns in Shane Stanley’s book. I’m loading data from a plist. The plist stores artist name and genre and other info. I have two buttons “Rock”, “Blues”. When user clicks a button, data corresponding to that genre is loaded in the table. This part works fine. I then added a NSSearchField described on Page 44 of the book which filters based on Artist Names. The issue is that when I click either the Rock or Blues button when the table is displaying filtered data, the removeObjects method of ArrayController removes only the filtered entries, not all objects . As a result I see duplicate data in my table

here is my code :


property thePlistData : {}
property PLIST_PATH : ""

on awakeFromNib()
        set plistFileString to (path to desktop as string) &  "artist_genre.plist"
        set PLIST_PATH to POSIX path of plistFileString
        set thePlistData to NSMutableDictionary's alloc()'s initWithContentsOfFile_(PLIST_PATH)
   end awakeFromNib

on loadArtists_(sender)
        set tagNumber to sender's tag() as string
        if tagNumber is equal to "1" then
            set artistsGenre to "Rock"
        else
            set artistsGenre to "Blues"
        end if
        -- build list of records
        set tempList to {}
        set theKeys to thePlistData's allKeys() as list
        repeat with key in theKeys
                set theDictObj to thePlistData's valueForKey_(key)
                set genreType to (theDictObj's valueForKey_("theGenre")) as string
                if genreType is equal to artistsGenre then
                    set end of tempList to {theArtist:key,theGenre: genreType}
                end if
        end repeat
        theArtistArrayController's removeObjects:(theArtistArrayController's arrangedObjects())
        theArtistArrayController's addObjects:tempList

    end loadArtists_

Is there a way to handle this issue? Is there another method to remove all objects and not just the filtered objects from the ArrayController?

The problem is that arrangedObjects() returns only visible objects. In this case you need to talk to the property itself. Probably like this:

set my thePlistData to NSMutableDictionary's dictionaryWithDictionary_(tempList)

Thanks for the suggestion Shane. But where would this statement go?

set my thePlistData to NSMutableDictionary's dictionaryWithDictionary_(tempList)

It would replace the last two lines of your handler.

I think I got it to work by clearing the Search field text and making objects visible before calling removeObjects

on loadArtists_(sender)
set tagNumber to sender's tag() as string
set currentSearchStr to searchField's stringValue() as string
-- Code....


if currentSearchStr is not equal to "" then
            searchFieldCell's setStringValue_("")
            theArtistArrayController's addObjects:tempList
end if

theArtistArrayController's removeObjects:(theArtistArrayController's arrangedObjects())
theArtistArrayController's addObjects:tempList

end loadArtists_

One last thing, I can clear the Search Field text, but the Cursor is still in the Search field along with the X button of the Search Field. Is there a way to programmatically click the X button of search field?
I did find a piece of code which is in objective-c, but I couldn’t convert it to ASOC.

[[[searchField cell] cancelButtonCell] performClick:self];

There’s an array controller setting, Clear Filter Predicate On Insert. That checkbox should do the trick.