I am in the process of making an app using CloudKit, but I have recently run into a problem. In my app, as soon as the main view controller loads, it goes off and fetches data from iCloud. The problem is that when I segue to another view controller and then back to the main view controller, it starts fetching again. I would like to be able to use the already fetched data, is there any way I could do this? Maybe I temporarily save it into CoreData?
There are a couple ways to approach this problem.
You will want to pass your data and the context from one view controller to the next. Make sure you remove the code in your second view controller the refetches the data.
The way I've chosen to do it is to pass the data from one to the next in the prepareForSegue function like so.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "mysegue"
{ let destinationViewController = (segue.destinationViewController as! UITabBarController) as! SecondViewController
destinationViewController.myData = myData
destinationViewController.context = stack.context
}
}