ios - NSFetchedResultsController notifies its Delegate of delete changes when a managed object is modified, and never notifies for Insert or Update -
i have uitableviewcontroller
, delegate nsfetchedresultscontroller
. nsfetchedresultscontrollerdelegate
functions set per "typical use" in apple's documentation, table view controller fetched result controller's delegate property.
i have view controllers presented on top of table view managed objects can modified. these modifications done on same managed object context. there 1 managed object context constructed in application, , accessed globally. (i have put print
statements in object context construction sure not accidentally re-constructing elsewhere.)
when modify 1 of managed objects, delegate function controller:didchangeobject
called, nsfetchedresultschangetype
always .delete
. when create managed object, delegate function not fire @ all.
however, when manually call call performfetch()
, tableview.reloaddata()
, cells restored correct state: removed row comes back, not-inserted rows created.
the result deleting object works expected (the cell removed), updates object cause cell removed, , object creations not trigger cell inserts.
i tried create simple demo of behaviour, when re-created situation blank application, don't see behaviour. within application causing strange behaviour, can't figure out what. ideas?
extra info:
the actual construction of predicate , sort descriptors done on several different classes, printing them via print(resultscontroller.fetchrequest.predicate)
, print(resultscontroller.fetchrequest.sortdescriptors)
gives following:
optional(readstate == "2" or readstate == "1")
optional([(readstate, ascending, compare:), (title, ascending, compare:)])
i have put print statement in controller:didchangeobject:
method, , can see gets called type.rawvalue = 2
(i.e. .delete
), , when modify objects, not when create them.
it's inconsistency how nsfetchedresultscontroller
handles nspredicate
.
if nsfetchedresultscontroller
constructed fetch request has predicate comparison between integer , string follows:
let predicate = nspredicate(format: "integerattribute == %@", string(1))
this lead predicate string being:
integerattribute == "1"
when case, initial fetches work fine: calling function performfetch()
on fetched results controller returns objects integerattribute
equal 1
(where integerattribute
of type int32
).
however, notifications nsfetchedresultscontrollerdelegate
not work fine. modifications of managed objects result in delegate being notified of nsfetchedresultschangetype.delete
change. creations of managed objects not invoke delegate @ all.
to make weirdness go away, fix predicate format string follows:
let predicate = nspredicate(format: "integerattribute == %d", 1)