samedi 27 juin 2015

How would I optimize this search function?

I'm having a tough time thinking through how I can make my search function more optimal. So I have a situation where I load an array of objects from a SQLITE database. Essentially one element in the array has multiple properties that can be accessed. Inside one element of the array, you have properties like time and rooms. I want to make the search flexible without having the user to filter what they are searching for. What I don't like about my implementation is that I filter through both time and room, then I for loop the filtered arrays and add them to a general "searchArrayToDisplay." I think the for loop is wasteful. I was wondering if there is a way I can append the filtered elements to the general "searchArrayToDisplay" without having to for loop? The method below is generally fast, but I think there's a better way that I'm just not seeing. Hopefully I am clear. Thanks for any help!

let filteredTime = searchArray.filter { (time: ProductModel) -> Bool in
               if time.customText1 == nil {
                 return false
                  } else {
                  return  time.customText1.rangeOfString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil, locale: nil) != nil

                }   
            }
            var m : ProductModel
            for m in filteredTime {

                searchArrayToDisplay.append(m)

            }

            let filteredRooms = searchArray.filter { (session: ProductModel) -> Bool in

                if session.roomName == nil {
                    return false
                } else {
                return session.roomName.rangeOfString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil, locale: nil) != nil
                }   
            }
            for pm in filteredRooms {
                searchArrayToDisplay.append(pm)
            }

Aucun commentaire:

Enregistrer un commentaire