I am making a simple task app using core data where in the main view controller I show list of task and there is bar button which takes me to a new view controller where I add new task to list the problem is
let fetchReq: NSFetchRequest<MoneyOut> = MoneyOut.fetchRequest()
do{
myArr = try container.viewContext.fetch(fetchReq)
}catch{
print(error)
}
OperationQueue.main.addOperation {
print(self.myArr)
self.myTableView.reloadData()
}
this works fine in main view controller but when I add a new task to list and come back to this main view controller my list is not updated I am using the code in view did load I know if I use this code in view will appear it will work just fine but the problem is i don't want to fetch all the data again when I have already fetched all data except the new data I just added is there a way we can do this
now I know the answer, well one of the method is to use Notification Center, as soon as core data context get saved you call
NotificationCenter.default.addObserver(self, selector: #selector(yourfunc), name: NSNotification.Name.NSManagedObjectContextDidSave, object: nil)
in place of yourfunc you write what you want to do, one of the thing you can do is fetch the data again and reload your table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.