When a user scrolls down to a certain percentage of the page, I postback to my database for more data. However, when updating the subview with data I notice a slight freeze in the scroll. I'm not sure what I'm doing wrong since all the calls are asynchronous
func PostBackAsync(PassURL:String, username:String, completion: (jsonData: NSDictionary?, error: NSError?)->Void) {
let post:NSString = "username=\(username)";
let url:NSURL = NSURL(string:PassURL)!
let postData = post.dataUsingEncoding(NSUTF8StringEncoding)!
let postLength = String(postData.length)
//Setting up `request` is similar to using NSURLConnection
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {urlData, response, reponseError in
if let receivedData = urlData {
let res = response as! NSHTTPURLResponse!;
NSLog("Response code: %ld", res.statusCode);
if (res.statusCode >= 200 && res.statusCode < 300) {
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(receivedData, options: []) as! NSDictionary
//On success, invoke `completion` with passing jsonData.
completion(jsonData: jsonData, error: nil)
} catch {
//On error, invoke `completion` with NSError.
completion(jsonData: nil, error: nil)
} }
else
{
completion(jsonData: nil, error: nil)
}
}
}
task.resume()
}
func PostBack(PassURL:String, username:String) {
PostBackAsync(PassURL, username: username) {jsonData, error in
if let json = jsonData {
dispatch_async(dispatch_get_main_queue(), {
let success:NSInteger = json.valueForKey("success") as! NSInteger
if(success == 1)
{
for var index=0; index < myArray.count; index++
{
//Do some Data Manipulation...
//..............
self.newView.addSubview(a[index] as! String)
self.newView.addSubview(b[index] as! String)
self.myScrollView.contentSize = CGSize(width: aWidth, height: myscrollViewContentSize)
//newView is the main subview inside of the scrollview
self.newView.frame = CGRectMake(0, 0, self.myScrollView.frame.width, myscrollViewContentSize)
}
}
}) }}
Does anything stand out as to why the UIScrollview scroll freezes slightly while this information is being updated?