1

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?

SlopTonio
  • 1,105
  • 1
  • 16
  • 39
  • you need to ensure that you're calling your function often enough so that there is always data preloaded for the current position in the table + X more cells. even then, depending on how you're drawing your cells and preparingForReuse, it can cause stutters – Louis Tur Dec 03 '15 at 20:21
  • @LouisTur im just adding new data to the bottom of the scroll using the origin of the last item + 30. Then I adjust the contentsize accordingly. This is not a tableview so im not using cells...? – SlopTonio Dec 03 '15 at 20:25
  • Why not just work around it by waiting until the scroll view is no longer scrolling before you update its contents? – matt Dec 04 '15 at 01:35
  • @matt because I want to avoid having the user reach the bottom of the scrollview unless there isn't anymore data from the db. Im updating the scrollview with more content every time the user reaches 70 percent of the way down – SlopTonio Dec 04 '15 at 01:40

1 Answers1

0

You may need to define the thread you want the async operation to run on to ensure it's not running on the UI thread. See this answer: https://stackoverflow.com/a/16283719/2347118

Community
  • 1
  • 1
Matt Mombrea
  • 6,793
  • 2
  • 20
  • 20
  • I've attempted to put dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { on multiple places but I'm still having the same issue and/or creating others . Any idea on where to put this worker thread? – SlopTonio Dec 04 '15 at 01:30
  • `DISPATCH_QUEUE_PRIORITY_DEFAULT` But that would be totally wrong. Updating the interface must be done on the _main thread_. – matt Dec 04 '15 at 01:41
  • @matt I'm currently using dispatch_async(dispatch_get_main_queue(), { in my original code but im getting a 1-2 second freeze during the scroll while the view is getting updated. – SlopTonio Dec 04 '15 at 01:43
  • @SlopTonio do you know for certain that the freeze is occurring when the view is being updated? Or is it possibly when then data fetch is triggered? Are you using reusable cells properly to avoid building the subview for each cell? – Matt Mombrea Dec 04 '15 at 16:24