ios - Why is UICollectionView reloadData crashing my app? -


i using uicollectionview display photos , have button allows user delete selected photo.

it works unless try delete last photo in array of photos being used populate uicollectionview. ie if there 5 photos there problem if user removes 5th photo not 1st, 2nd, 3rd or 4th. when try delete 5th photo crashes on reloaddata() following error

terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'attempt delete item 4 section 0 contains 4 items before update'

i don't understand why happen... error mentions "attempt delete" never told deleting anything. changed source , asked update. in error message says section contains 4 items before update when 5 photos. why?

a little bit more info i'm doing , how (using swift)...

i've got progressphotosheetclass have instantiated object progressphotosheet

this progressphotosheet object has array of photos , photos can have priorities such photo id, title etc

in number of items in section use

var numpics: int = progressphotosheet.progressphotos.count return numpics 

in cellforitematindexpath use

let cell = collectionview.dequeuereusablecellwithreuseidentifier(reuseidentifier, forindexpath: indexpath) as! photoholder cell.phototitle.text = progressphotosheet.progressphotos[indexpath.row].photoname 

...etc..

when deleting item use

progressphotosheet.deletephoto(progressphotosheet.progressphotos[indexpath.row].photoid)

which deletes photo device , core data , reloads progressphotosheet.progressphotos using modified core data how before without deleted photo.

i call

self.collectionview.reloaddata() 

which should update uicollectionview new data

i understand if felt there mismatch between should in collection view , in datasource if using self.collectionview.deleteitemsatindexpaths(indexpaths) because saying ignored them match need delete 1 item - here there possibility mismatch.. surely using self.collectionview.reloaddata() doesn't matter changes made should @ data there , update uicollectionview accordingly....

so question is... why getting error , should fix things don't it?

edit include more info

here telephoto code

func deletephoto(photoid: int) {      // set core data managed object context     let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate     let managedcontext = appdelegate.managedobjectcontext!        // fetch correct photo     let fetchrequest = nsfetchrequest(entityname: "cdprogressphoto")     fetchrequest.predicate = nspredicate(format: "photoid = %@", string(photoid))      // save     if let fetchresults = managedcontext.executefetchrequest(fetchrequest, error: nil) as? [nsmanagedobject] {        if fetchresults.count != 0{              // 1 photo photo id             var photo = fetchresults[0]              photo.setvalue(true, forkey: "todelete")              // save object             var error: nserror?             if !managedcontext.save(&error) {                 println("could not save \(error), \(error?.userinfo)")             }         }     }        // reload core data     self.loadphotosheetfromcoredata() } 

self.loadphotosheetfromcoredata() empties progressphotosheet.progressphotos before getting new data core data... code below...

private func loadphotosheetfromcoredata() {      if(self.hasphotosheet()) {          // clear existing photos         self.progressphotos = []          // set core data managed object context         let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate         let managedcontext = appdelegate.managedobjectcontext!          let request = nsfetchrequest(entityname: "cdprogressphoto")         let predicate1 = nspredicate(format: "photosheetid == %@", string(self.sheetid))         let predicate2 = nspredicate(format: "todelete == %@", false)         request.sortdescriptors = [nssortdescriptor(key: "date", ascending: false) nssortdescriptor]         var predicatesarray: [nspredicate] = [predicate1, predicate2]         //predicatesarray.append(predicate1)         request.predicate = nscompoundpredicate.andpredicatewithsubpredicates(predicatesarray)         let existings = managedcontext.executefetchrequest(request, error: nil)         let existingphotos: [cdprogressphoto] =  existings as! [cdprogressphoto]          // each photo make progressphoto object , add progress photos array         photo in existingphotos {             var newphoto: progressphoto = progressphoto()             newphoto.photosheetid = int(photo.photosheetid)             newphoto.photoid  = int(photo.photoid)             newphoto.photoname  = photo.photoname             newphoto.date = int(photo.date)             newphoto.url = photo.url             newphoto.filename = photo.filename             newphoto.height = float(photo.height)             newphoto.width = float(photo.width)             newphoto.selected = false              self.progressphotos.append(newphoto)         }      } } 

as can see photo isn't deleted @ point set todelete flag true , re load items todelete set false. photos deleted later asynchronously depending on network connection etc because stored on server use on main website.

have tried calling invalidatelayout() on collectionview? might incase view empty i.e. 0 elements present.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -