I am trying to build an app which is dependent on having some items in CoreData
. I have it syncing with an external data source, which all works fine.
my app uses three methods, and is a single view app:
syncData()
createSpinner()
showResult()
now createSpinner
is dependent on having some data in CoreData
- and only needs to run once
showResult
is dependent on the 'Spinner' having been created, and is called once on creation to initialize itself, as well as each time my spinner has been spun
I currently have SyncData
in the viewDidLoad()
, and the createSpinner()
in viewDidAppear()
(as it changes size depending on screen size)
The problem is on first launch the data does not load in time for the createSpinner()
, and thus the app looks useless. How can I 'wait' for that first sync, or set up something to check there is some data?
The solution is to force syncData()
& createSpinner()
to run in same thread
you can do this by creating a serial queue and dispatch both methods asynchronously into it
let serialQueue = dispatch_queue_create("com.mycompany.myview", DISPATCH_QUEUE_SERIAL);
override func viewDidLoad() {
super.viewDidLoad()
dispatch_async(serialQueue) {
syncData()
}
}
override func viewDidAppear() {
super.viewDidAppear()
dispatch_async(serialQueue) {
createSpinner()
}
}