Hello there,
I'm a Swift beginner and I'm currently stuck on the following problem. It'd be incredibly helpful if I could be pointed in the right direction or got some sort of feeback:
I'm developing an application following an MVC (Model, View, Controller) model/architecture. Once a specific button is tapped, the Model module (basically a separated class that contains my app's "brain") of the application coonects to a server and does a bunch of stuff there (which is not important in this context). When it is done, it calls back a function that was passed as an argument to the "brain class" and resides inside the ViewController. As a test, I'm setting the alpha of a ViewController's UITextField to zero: if I do that when the button is tapped, that works fine. However, if I try to set it inside the callback function, nothing happens (but I have checked and the function is, in fact, being called).
I suspect it has something to do with threads or maybe the callback function is being copied over and, because of that, is not able to set properties from the ViewController's items.
A few more details regarding my application's logic:
- DBService is the class responsible for the database connection. The dBService.saveUser() function gets called from the ViewController, and the callback function is passed as a parameter. The following line is called inside an @IBAction:
dBService.saveUser(student, successfulSignupViewControllerCompletionHandler: self.signUpSuccessful)Here is the callback:
private func signUpSuccessful() {
// self.textField.alpha = 0
self.dismissViewControllerAnimated(true, completion: nil)
}This works and the view is dismissed, but If I uncomment the first line and comment the second, the alpha is not set.
Inside the dBService's class, however, the callback function gets passed around a little bit: the saveUser() function calls an internal function called request(), passing the callback function as a parameter:
func saveUser(student: Student, successfulSignupViewControllerCompletionHandler: (() -> Void)!) {
request(student, successViewControllerHandler: successfulSignupViewControllerCompletionHandler)
}Here's the request function:
private func request (student: Student, successViewControllerHandler: (() -> Void)!) {
// ... a bunch of NSURL stuff commented out to simplify
dispatch_async(dispatch_get_main_queue(), {
self.requestCompletionHandler(statusValue, messageValue: messageValue, error: nil, successViewControllerHandler: successViewControllerHandler)
// ...
}That's it... basically, if I try to do a println() inside the ViewController's callback function, for example, it works; trying to set visual componnent's properties won't.
I found a similar question somewhere in the discussion forums, but it did not help me. Any help would be appreciated. Thanks!