swift - UISearchController returns only first results but Core Data has several results -


i have uisearchcontroller , returns first results core data has several results.

i did many different variants didn't me.

else uisearchcontroller returns incorrect results.

import uikit import foundation import coredata  class songtablevc: uitableviewcontroller, uitableviewdatasource, uitableviewdelegate, nsfetchedresultscontrollerdelegate, uisearchcontrollerdelegate, uisearchbardelegate, uisearchresultsupdating {      // mark: - var , let     var appdel = (uiapplication.sharedapplication().delegate as! appdelegate)     var context = (uiapplication.sharedapplication().delegate as! appdelegate).managedobjectcontext!       // mark: - nsfetchedresultscontroller , functions     var fetchedresultscontroller: nsfetchedresultscontroller!      func returnrequest() -> nsfetchrequest {         var fetchrequest = nsfetchrequest(entityname: "song")         var sort = nssortdescriptor(key: "songname", ascending: false)         fetchrequest.fetchbatchsize = 50         fetchrequest.predicate = nil         fetchrequest.sortdescriptors = [sort]         return fetchrequest     }      // mark: - uisearchcontroller , fucntions     var searchcontroller: uisearchcontroller!     var searchpredicate: nspredicate!     var datafiltered: [song]? = nil      func updatesearchresultsforsearchcontroller(searchcontroller: uisearchcontroller) {         var searchtext = searchcontroller.searchbar.text         searchpredicate = nspredicate(format: "songname contains[c] %@", searchtext)         datafiltered = self.fetchedresultscontroller?.fetchedobjects?.filter(){             return self.searchpredicate!.evaluatewithobject($0)         } as! [song]?         self.tableview.reloaddata()         println(searchpredicate)     }      func searchbar(searchbar: uisearchbar, selectedscopebuttonindexdidchange selectedscope: int) {         updatesearchresultsforsearchcontroller(searchcontroller)     }      func diddismisssearchcontroller(searchcontroller: uisearchcontroller) {         searchpredicate = nil         datafiltered = nil         self.tableview.reloaddata()     }        override func viewdidload() {         super.viewdidload()         self.navigationitem.leftbarbuttonitem = self.editbuttonitem()          fetchedresultscontroller = nsfetchedresultscontroller(fetchrequest: returnrequest(), managedobjectcontext: context, sectionnamekeypath: "songname", cachename: "songname")         fetchedresultscontroller.delegate = self         fetchedresultscontroller.performfetch(nil)          searchcontroller = ({             var controllersearch = uisearchcontroller(searchresultscontroller: nil)             controllersearch.delegate = self             controllersearch.searchbar.delegate = self             controllersearch.searchbar.sizetofit()             controllersearch.definespresentationcontext = false // default false             controllersearch.hidesnavigationbarduringpresentation = true             controllersearch.searchresultsupdater = self             controllersearch.dimsbackgroundduringpresentation = false              self.tableview.tableheaderview = controllersearch.searchbar             return controllersearch         })()     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }      // mark: - table view data source      override func numberofsectionsintableview(tableview: uitableview) -> int {         // #warning potentially incomplete method implementation.         // return number of sections.         if searchpredicate == nil {             return fetchedresultscontroller?.sections?.count ?? 0         } else {             return 1 ?? 0         }     }      override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {         if searchpredicate == nil {             return fetchedresultscontroller?.sections?[section].numberofobjects ?? 0         } else {             return datafiltered?.count ?? 0         }     }      override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         let cell = tableview.dequeuereusablecellwithidentifier("songid", forindexpath: indexpath) as! uitableviewcell          if searchpredicate == nil {             if var dataforcell = fetchedresultscontroller.objectatindexpath(indexpath) as? song {                 cell.textlabel?.text = dataforcell.songname                 cell.detailtextlabel?.text = dataforcell.songname             } else {                 if var datafilterforcell = datafiltered?[indexpath.row] {                     cell.textlabel?.text = datafilterforcell.songname                     cell.textlabel?.text = datafilterforcell.songname                 }             }         }         return cell     }       // override support conditional editing of table view.     override func tableview(tableview: uitableview, caneditrowatindexpath indexpath: nsindexpath) -> bool {         // return no if not want specified item editable.         return true     }      // override support editing table view.     override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) {         if editingstyle == .delete {             context.deleteobject(fetchedresultscontroller.objectatindexpath(indexpath) as! nsmanagedobject)             context.save(nil)         } else if editingstyle == .insert {             context.insertobject(fetchedresultscontroller.objectatindexpath(indexpath) as! nsmanagedobject)             context.save(nil)         }         }      func controller(controller: nsfetchedresultscontroller, didchangesection sectioninfo: nsfetchedresultssectioninfo, atindex sectionindex: int, forchangetype type: nsfetchedresultschangetype) {         var tableview = uitableview()         if searchpredicate == nil {             tableview = self.tableview         } else {             tableview = (searchcontroller.searchresultscontroller as! songtablevc).tableview         }          switch type {         case nsfetchedresultschangetype.insert:             tableview.insertsections(nsindexset(index: sectionindex), withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.delete:             tableview.deletesections(nsindexset(index: sectionindex), withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.move:             tableview.deletesections(nsindexset(index: sectionindex), withrowanimation: uitableviewrowanimation.fade)             tableview.insertsections(nsindexset(index: sectionindex), withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.update:             break         default: break         }     }      func controller(controller: nsfetchedresultscontroller, didchangeobject anobject: anyobject, atindexpath indexpath: nsindexpath?, forchangetype type: nsfetchedresultschangetype, newindexpath: nsindexpath?) {         var tableview = uitableview()         if searchpredicate == nil {             tableview = self.tableview         } else {             tableview = (searchcontroller.searchresultscontroller as! songtablevc).tableview         }          switch type {         case nsfetchedresultschangetype.insert:             tableview.insertrowsatindexpaths([anyobject](), withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.delete:             tableview.deleterowsatindexpaths(nsarray(object: indexpath!) [anyobject], withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.move:             tableview.deleterowsatindexpaths(nsarray(object: indexpath!) [anyobject], withrowanimation: uitableviewrowanimation.fade)             tableview.insertrowsatindexpaths(nsarray(object: indexpath!) [anyobject], withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.update:             tableview.cellforrowatindexpath(indexpath!)             break         default: break         }     }      func controllerwillchangecontent(controller: nsfetchedresultscontroller) {         if searchpredicate == nil {             tableview.beginupdates()         } else {            (searchcontroller.searchresultscontroller as? songtablevc)?.tableview.beginupdates()         }     }      func controllerdidchangecontent(controller: nsfetchedresultscontroller) {         if searchpredicate == nil {             tableview.endupdates()         } else {             (searchcontroller.searchresultscontroller as? songtablevc)?.tableview.endupdates()         }     }      // mark: - navigation      // in storyboard-based application, want little preparation before navigation     override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {         if segue.identifier == "add" {             searchcontroller.active == false          }     }  } 

enter image description here enter image description here

i found solution of question

import uikit import foundation import coredata  class songtableviewcontroller: uitableviewcontroller, uitableviewdatasource, uitableviewdelegate, nsfetchedresultscontrollerdelegate, uisearchcontrollerdelegate, uisearchbardelegate, uisearchresultsupdating {      // mark: - var , lets     var appdel = (uiapplication.sharedapplication().delegate as! appdelegate)     var context = (uiapplication.sharedapplication().delegate as! appdelegate).managedobjectcontext!      override func viewdidload() {         super.viewdidload()         fetchedresultscontroller = nsfetchedresultscontroller(fetchrequest: fetchrequest(), managedobjectcontext: context, sectionnamekeypath: "namesong", cachename: "namesong")         fetchedresultscontroller.delegate = self         fetchedresultscontroller.performfetch(nil)          self.navigationitem.leftbarbuttonitem = self.editbuttonitem()          searchcontroller = ({             var controllersearch = uisearchcontroller(searchresultscontroller: nil)             controllersearch.delegate = self             controllersearch.searchbar.delegate = self             controllersearch.hidesnavigationbarduringpresentation = true             controllersearch.definespresentationcontext = false             controllersearch.dimsbackgroundduringpresentation = false             controllersearch.searchbar.sizetofit()             controllersearch.searchresultsupdater = self             self.tableview.tableheaderview = controllersearch.searchbar             return controllersearch         })()          //          println(path)     }      override func viewdidappear(animated: bool) {         super.viewdidappear(animated)         searchpredicate = nil         filtereddata = nil         self.tableview.reloaddata()     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }      // mark: - nsfetchedresultscontroller , functions     var fetchedresultscontroller: nsfetchedresultscontroller!      func fetchrequest() -> nsfetchrequest {         var fetchrequest = nsfetchrequest(entityname: "song")         var sort = nssortdescriptor(key: "namesong", ascending: false)         fetchrequest.fetchbatchsize = 50         fetchrequest.predicate = nil         fetchrequest.sortdescriptors = [sort]         return fetchrequest     }      // mark: - uisearchcontroller , functions     var searchcontroller: uisearchcontroller!     var searchpredicate: nspredicate!     var filtereddata: [song]? = nil      func updatesearchresultsforsearchcontroller(searchcontroller: uisearchcontroller) {         var searchtext = searchcontroller.searchbar.text         if searchtext != nil {             searchpredicate = nspredicate(format: "namesong contains[c] %@", searchtext)             filtereddata = fetchedresultscontroller.fetchedobjects!.filter() {                 return self.searchpredicate.evaluatewithobject($0)             } as? [song]             self.tableview.reloaddata()         }     }      func searchbar(searchbar: uisearchbar, selectedscopebuttonindexdidchange selectedscope: int) {         updatesearchresultsforsearchcontroller(searchcontroller)     }      func diddismisssearchcontroller(searchcontroller: uisearchcontroller) {         searchpredicate = nil         filtereddata = nil         self.tableview.reloaddata()     }      // mark: - files shared folder      var filemanager = nsfilemanager.defaultmanager()       var path = nssearchpathfordirectoriesindomains(nssearchpathdirectory.documentdirectory, nssearchpathdomainmask.userdomainmask, true)        // mark: - table view data source      override func numberofsectionsintableview(tableview: uitableview) -> int {         if searchpredicate == nil {             return fetchedresultscontroller?.sections?.count ?? 0         } else {             return 1 ?? 0         }     }      override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {         if searchpredicate == nil {             return fetchedresultscontroller?.sections?[section].numberofobjects ?? 0         } else {             return filtereddata?.count ?? 0         }     }      override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         let cell = tableview.dequeuereusablecellwithidentifier("songid", forindexpath: indexpath) as! uitableviewcell          if searchpredicate == nil {             if var dataforcell = fetchedresultscontroller?.objectatindexpath(indexpath) as? song {                 cell.textlabel?.text = dataforcell.namesong             }         } else {             if var filteredsearch = filtereddata?[indexpath.row] {                 cell.textlabel?.text = filteredsearch.namesong             }         }          return cell     }      // override support conditional editing of table view.     override func tableview(tableview: uitableview, caneditrowatindexpath indexpath: nsindexpath) -> bool {         // return no if not want specified item editable.         return true     }      // override support editing table view.     override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) {         if editingstyle == .delete {             context.deleteobject(fetchedresultscontroller.objectatindexpath(indexpath) as! nsmanagedobject)             context.save(nil)         } else if editingstyle == .insert {             context.insertobject(fetchedresultscontroller.objectatindexpath(indexpath) as! nsmanagedobject)             context.save(nil)         }         }      func controller(controller: nsfetchedresultscontroller, didchangesection sectioninfo: nsfetchedresultssectioninfo, atindex sectionindex: int, forchangetype type: nsfetchedresultschangetype) {         switch type {         case nsfetchedresultschangetype.insert:             tableview.insertsections(nsindexset(index: sectionindex), withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.delete:             tableview.deletesections(nsindexset(index: sectionindex), withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.move:             tableview.deletesections(nsindexset(index: sectionindex), withrowanimation: uitableviewrowanimation.fade)             tableview.insertsections(nsindexset(index: sectionindex), withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.update:             break         default: break          }     }      func controller(controller: nsfetchedresultscontroller, didchangeobject anobject: anyobject, atindexpath indexpath: nsindexpath?, forchangetype type: nsfetchedresultschangetype, newindexpath: nsindexpath?) {         switch type {         case nsfetchedresultschangetype.insert:             tableview.insertrowsatindexpaths([anyobject](), withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.delete:             tableview.deleterowsatindexpaths(nsarray(object: indexpath!) [anyobject], withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.move:             tableview.deleterowsatindexpaths(nsarray(object: indexpath!) [anyobject], withrowanimation: uitableviewrowanimation.fade)             tableview.insertrowsatindexpaths(nsarray(object: indexpath!) [anyobject], withrowanimation: uitableviewrowanimation.fade)             break         case nsfetchedresultschangetype.update:             tableview.cellforrowatindexpath(indexpath!)             break         default: break         }     }      func controllerdidchangecontent(controller: nsfetchedresultscontroller) {         tableview.endupdates()     }      func controllerwillchangecontent(controller: nsfetchedresultscontroller) {         tableview.beginupdates()     }      /*     // override support rearranging table view.     override func tableview(tableview: uitableview, moverowatindexpath fromindexpath: nsindexpath, toindexpath: nsindexpath) {      }     */      // override support conditional rearranging of table view.     override func tableview(tableview: uitableview, canmoverowatindexpath indexpath: nsindexpath) -> bool {         // return no if not want item re-orderable.         return true     }      /*     // mark: - navigation      // in storyboard-based application, want little preparation before navigation     override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {         // new view controller using [segue destinationviewcontroller].         // pass selected object new view controller.     }     */  } 

Comments