Reputation: 449
The title of my question makes this look like a duplicate, but it's much more complicated of an issue than the title suggests, so please read through this post before marking it as a duplicate.
I have a tableview that uses UIRefresh for pull-to-refresh. When the tableview refreshes, it gets info from CloudKit. As the first answer to this post (Swift pull to reload duplicates objects in table instead of updating) suggests, I clear my old list before loading data from CloudKit. This means that, while the UIRefresh is active, my tableview goes through three states:
[populated with old data] -> [no data at all] -> [populated with new data]
If the user tries to tap a cell during the second phase, unsurprisingly the app crashes because the view controller I segue to depends on the info from the cloud.
I can think of 3 potential solutions to this problem, and I am hoping to get advice from more experienced iOS programmers on what the best approach would be (if any of these).
1) Save the old table list so that if the user taps a cell in phase 2 of the loading process, I can somehow have the app access that list.
2) Don't let the user tap cells while UIRefresh is active. (This seems unideal because I'd like to add an auto-refresh feature at some point.)
3) As the StackOverflow post linked to above mentioned in its second answer, I could pull down from the cloud only the most recently modified items...but this, I think, would be messy for my situation because any of the cells could have been modified, and all of them will most likely need to be refreshed in my case.
Any advice on best practices here would be much appreciated! Thanks!
Upvotes: 0
Views: 88
Reputation: 306
My suggestion would be to only modify your data once the new data arrives. After replacing the data immediately call tableView.reloadData()
Upvotes: 1
Reputation: 449
My solution #1 seems to work pretty well. Not sure if there are pitfalls to it that I haven't discovered yet, but it seems to work pretty well.
Upvotes: 0