Reputation: 97
I am working on an application that retrieves a user's friends from a database and outputs then in a table view.
I have successfully coded the table view to the point where it reads the indexes I insert into an NSMutableArray(). My thinking is to use NSURL to send a query to a MySQL database via URL variables with PHP.
I have used NSURL many times to interact with the database, however when I use it in the viewDidLoad() function to load the friends immediately on the application's load, it crashes but does not return an error.
Code:
class viewFriendsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var textArray: NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
let myUrl = NSURL(string: "http://www.casacorazon.org/ios.html")
let request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
if error != nil {
print("Error: \(error)")
}
dispatch_async(dispatch_get_main_queue()) {
print(responseString)
}
}
}
task.resume()
//get username from NSUserDefaults
//if username inavailable, insert error report into first row
//use PHP script to get friends from user's database
//split return string by '9245203598' into array
//load split array into NSMutableArray via foreach loop
//let username = NSUserDefaults.standardUserDefaults().stringForKey("username")*/
self.textArray.addObject("First Index")
self.textArray.addObject("Second Index")
self.textArray.addObject("Third Index")
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44.0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.textArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.text = self.textArray.objectAtIndex(indexPath.row) as? String
return cell
}
func sendAlert(subject: String, message: String) {
let alertController = UIAlertController(title: subject, message:
message, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
Upvotes: 1
Views: 80
Reputation: 169
because your app complete excuting the code in UI before the background finish its task. you have to ensure that background task is completed then continue in UI.
Note: NSUrlsesstion is running in background you don't have to import dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {} inside it.
Upvotes: 2