ios - Remove data from core data using UILocalNotification -
i trying remove data code data model when local notification fired. notifications's alertbody
,then fetch sort data using notification title :
func application(application: uiapplication, handleactionwithidentifier identifier: string?, forlocalnotification notification: uilocalnotification, completionhandler: () -> void) { if identifier == "deleteevent" { context = coredatastack.managedobjectcontext { request = nsfetchrequest(entityname: "event") let titlepredicate = nspredicate(format: "title contains[c] %@" ,notification.alertbody!) request.predicate = titlepredicate results = try context.executefetchrequest(request) print(results.count) // returns 1 } catch { print("error") } { results.removeatindex(0) coredatastack.savecontext() nsnotificationcenter.defaultcenter().postnotificationname("reloadtableview", object: nil) print(results.count) // returns 0 } } completionhandler() }
when remove data model , go event view controller example still can see data there ! missing ?! thanks.
removing element results
array (using removeatindex
) not delete persistent store - or context. need tell context delete object:
let object = results[0] as! nsmanagedobject context.deleteobject(object)
Comments
Post a Comment