ios - NSFetchedResultsController: no object at index 2147483647 -
need hint, give after spending several hours struggling nsfetchedresultscontroller.
the error message is:
coredata: error: nsfetchedresultscontroller: no object @ index 2147483647 in section @ index 0
...but don't know firing error. last piece of code savecontext(), next breakpoint inside didchange.
class viewcontroller : uitableviewcontroller, nsfetchedresultscontrollerdelegate private lazy var channelcontroller: nsfetchedresultscontroller<zchannel> = { let appdelegate: appdelegate = uiapplication.shared.delegate as! appdelegate let request: nsfetchrequest<zchannel> = zchannel.fetchrequest() request.sortdescriptors = [nssortdescriptor(key: "kit", ascending: true), nssortdescriptor(key: "name", ascending: true)] let retval: nsfetchedresultscontroller<zchannel> = nsfetchedresultscontroller(fetchrequest: request, managedobjectcontext: appdelegate.persistentcontainer.viewcontext, sectionnamekeypath: "kit", cachename: nil) retval.delegate = self return retval }() public init() { super.init(style: .grouped) self.tableview.register(channeltablecell.self, forcellreuseidentifier: "channeltablecell") let appdelegate: appdelegate = uiapplication.shared.delegate as! appdelegate appdelegate.persistentcontainer.viewcontext.perform { { try self.channelcontroller.performfetch() } catch { let e = error nserror fatalerror("[coredata] unresolved fetch error \(e), \(e.userinfo)") } } } public func controller(_ controller: nsfetchedresultscontroller<nsfetchrequestresult>, didchange anobject: any, @ indexpath: indexpath?, type: nsfetchedresultschangetype, newindexpath: indexpath?) { switch(type) { case .insert: self.tableview?.insertrows(at: [newindexpath!], with: .bottom) break; case .update: self.tableview?.reloadrows(at: [indexpath!], with: .bottom) break default: break } } public func controller(_ controller: nsfetchedresultscontroller<nsfetchrequestresult>, didchange sectioninfo: nsfetchedresultssectioninfo, atsectionindex sectionindex: int, type: nsfetchedresultschangetype) { switch (type) { case .insert: self.tableview?.insertsections(indexset(integer: sectionindex), with: uitableviewrowanimation.bottom) break case .delete: self.tableview?.deletesections(indexset(integer: sectionindex), with: uitableviewrowanimation.bottom) break default: break } }
from other thread insert new object:
self.persistentcontainer.viewcontext.performandwait { // ... let channel: zchannel = nsentitydescription.insertnewobject(forentityname: "zchannel", into: self.persistentcontainer.viewcontext) as! zchannel // ... self.savecontext() }
some issues:
- 2147483647 nsnotfound. if using
indexofobject
, assuming in array , using index cause of crash. - you using indexpath row update when should use newindexpath. reason indexpath index before inserts or delete , newindexpath index after inserts , deletes.
- there no reason call
self.channelcontroller.performfetch()
insidepersistentcontainer.viewcontext.perform
on main thread
Comments
Post a Comment