DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > The Tale of the List Control, the Async Call, and the Layout Cycle Error

The Tale of the List Control, the Async Call, and the Layout Cycle Error

Refreshing a list of items was causing the app to crash when it was repopulated. Instead of banging his head on the wall, Colin Bacon came up with a solution.

$$anonymous$$ user avatar by
$$anonymous$$
·
Nov. 29, 16 · Web Dev Zone · Opinion
Like (1)
Save
Tweet
2.33K Views

Join the DZone community and get the full member experience.

Join For Free
The tale of the list control, the async call and the layout cycle error

This one had me banging my head against the wall for a long time, refreshing a list of items was causing the app to crash when it was repopulated.

Disclaimer: I see my solution as no more than a hack, but a necessary one.

Setting the Scene

I had an ObservableCollection of user actions that were populated when the user tapped on the item in another list. A bit like a master detail approach I guess. The items displayed would vary and the list could be cleared and repopulated. Further to that the items came from multiple async requests to an external service that could come back at different times in different orders. Hence the need for an ObservableCollection.

Populating the list the first time was not a problem, however repopulating that list would crash the app with this error:

Layout cycle detected. Layout could not complete.

My exact steps were:

  1. Populating the list with three separate async requests
  2. Clearing the list of items
  3. Doing step 1 again

Understanding the Error

I won't claim to fully understand it, but from what I learnt (Googled), a layout cycle error is a UI error where the layout is unable to complete because there is an infinite loop. This can be caused by a control handling event changes such as size that just get stuck by continually firing. This stack overflow answer has some more detail.

Narrowing It Down

I was pretty sure it was the control I was using to display the items that was causing the problem. I was using ItemsControl. Changing to ListBox or ListView made no difference.

Finding the Cause

After a lot of hair pulling and general cursing this comment in a Xaramin forum gave me a clue to what was causing it. Async calls.

To retrieve user action three async calls were being made each updating the ObservableCollection with items. I took two of the calls out so the list was only being repopulated once, the error stopped.

If this is the cause of your problem and you can re-engineer your solution to a single or synchronous request I would recommend that. Unfortunately, I was able to do neither.

The Hack

I had seen several suggestions that setting the size of the control would help with this error, as the layout was fixed and could be calculated. I had given up on finding a solution that seemed sensible to me so decided to give this a go. I could not set a fixed height as the list could vary in size. Instead, I set a MinHeight value to the height of one row. And it worked! Giving it a starting point was enough to stop the layout cycle error occurring and I could get on with my life.

Published at DZone with permission of $$anonymous$$, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Hard Things in Computer Science
  • SQL vs. NoSQL: Pros and Cons
  • Monolith vs Microservices Architecture: To Split or Not to Split?
  • What I Miss in Java, the Perspective of a Kotlin Developer

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo