So I'm adding several subviews to my ViewController's view in a loop; the problem is there's a ~5s delay for it in showing up from the time I add the subview. I made sure to call setNeedsDisplay on both the subview and my view after adding it, no dice. I even double-checked that the code is executing on the main thread using NSThread.isMainThread()
, and it returned true as expected. Here's my exact code:
dispatch_async(dispatch_get_main_queue()) {
var nib = UINib(nibName: "ProfileCard", bundle: NSBundle.mainBundle())
var profiles = self.delegate.user!.profilesToShow
//loop to add ProfileCard subviews
while profiles.count > 0 && self.deck.count < self.MAX_DECK_SIZE {
println("Adding Card")
//setup subview
var card = nib.instantiateWithOwner(self, options: nil).first! as ProfileCard
card.delegate = self
card.frame = CGRect(x: (self.view.bounds.width - 300)/2, y: 100, width: 300, height: 200)
card.displayProfile(profiles.first!)
//add subview
self.view.addSubview(card)
self.view.sendSubviewToBack(card)
card.setNeedsDisplay()
self.view.setNeedsDisplay()
//add card to deck, mark as shown
self.deck.append(card)
profiles.removeAtIndex(0)
}
}
Any print statements in this loop run about ~5s prior to the subview actually displaying, and I'm not sure what that's due to. I even set print statements in the init method of the subview, and even those printed with expected.
This code may not be running on a background thread, but the fact that you have to come onto the main thread suggests that you have other code that is running on a background thread. That other code is the source of the problem. It is trying to talk to the interface on the background thread. That in turn is gumming up the whole drawing system.
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.